Difference between list, sequence and slice in Python?

Question:

What are the differences between these built-in Python data types: list, sequence and slice? As I see it, all three essentially represent what C++ and Java call array.

Asked By: Tony the Pony

||

Answers:

  • lists are a sequence type, similar to an array

  • sequence types describe a functional superset:

There are six sequence types: strings, Unicode strings, lists, tuples, buffers, and xrange objects.

  • slices are a notation for subarrays (or substrings, also)

Read more … http://docs.python.org/glossary.html

Answered By: miku

Strictly speaking, a slice is a type which represents a range of indices, e.g. a start, stop, and a step. A slice isn’t a container type at all. You can use a slice to index an list, resulting in a new list which is a copy of a sublist of the original list.

Lists differ from C++ arrays in that they’re heterogenous; the elements are not required to be of the same type. And as MYYN has already pointed out, “sequence” isn’t a Python type at all but rather a description of a variety of built-in types.

Answered By: Peter Milley
  • list are more than plain arrays. You can initialize them without giving the number of items. You can append/push to them, you can remove/pop/del items from them, you can have lists of different types of objects (e.g., [1,'e', [3]]), you can have recursive lists… and you can slice lists, which means getting a new list with only a few of the items.
  • slice are an object type used “behind the scenes” to handle extended slicing in the a[start:stop:step] form, as help(slice) reveals.

“Sequence” is not an object, more like an informal interface some objects like list implement.

Answered By: badp

A list is a sequence but a sequence is not necessarily a list. A sequence is any type that supports the sequence interface (“protocol”). This is done by duck-typing rather than through a strict inheritance hierarchy. Note that sequences are containers, but containers are not necessarily sequences. (sequences are, well, sequential!)

See http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange

Slice objects are generally created implicitly via syntactic sugar (foo[2:5]) and provided to the container type special methods (such as __getitem__) which you can override. You will generally not have to deal with slices unless if you create your own sequences/containers.

See http://docs.python.org/reference/datamodel.html#specialnames

Lists are comparable to arrays. I’m not certain, but I think it’s implemented in cPython as a dynamically expanding array. However, the interface makes it so that it’s more like a C++ STL Vector than just a plain old array.

Answered By: Jeremy Brown

You’re mixing very different things in your question, so I’ll just answer a different question

You are now asking about one of the most important interface in Python: iterable – it’s basically anything you can use like for elem in iterable.

iterable has three descendants: sequence, generator and mapping.

  • A sequence is a iterable with random access. You can ask for any item of the sequence without having to consume the items before it. With this property you can build slices, which give you more than one element at once. A slice can give you a subsequence: seq[from:until] and every nth item: seq[from:until:nth]. list, tuple and str all are sequences.

  • If the access is done via keys instead of integer positions, you have a mapping. dict is the basic mapping.

  • The most basic iterable is a generator. It supports no random access and therefore no slicing. You have to consume all items in the order they are given. Generator typically only create their items when you iterate over them. The common way to create generators are generator expressions. They look exactly like list comprehension, except with round brackets, for example (f(x) for x in y). Calling a function that uses the yield keyword returns a generator too.

The common adapter to all iterables is the iterator. iterators have the same interface as the most basic type they support, a generator. They are created explicitly by calling iter on a iterable and are used implicitly in all kinds of looping constructs.

Answered By: Jochen Ritzel