Difference between list, sequence and slice in Python? Difference between list, sequence and slice in Python? python python

Difference between list, sequence and slice in Python?


You're mixing very different things in your question, so I'll just answer a different question ;-P

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.


  • 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.


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.