Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

When it comes to data manipulation, one often needs some structure to store multiple values. Python is no exception and it actually has several compound data types. The one described in this article is very similar to some other structures in other languages: the list. It can store multiple values from different types (so you could have a list with integers only or a mix with integer, floating numbers, or even lists of lists). A list is written with values, separated by commas and enclosed in square brackets.

Examples

For example, you might write:

A list of integers:

>>> integerList = [1, 2, 3, 4]

A list whose values are made up of different types:

>>> mixedList   = [1, "hello", 2.02, "foo"]

A list where some values are themselves list:

>>> listOfLists = [1, 2, [3, 4, 5], [5, 6, 7], 7, 8]

Indexing and slicing

Once you have a list, you might quickly need a way to access a given value in this list. Or sometimes, you might want a subset of it (a slice). Python's syntax allows for these operations quite simply.

Indexing

A given element of the list has an index which is its position regarding the start of the list. Given an element's index, it is possible to fetch the value in the list by writing the name of the list, followed by the index, enclosed in square brackets. Attention, indexes start from 0 so be careful when counting.

>>> myList = [1, 2, 3, 4, 5]
>>> print(myList[0])
1
>>> myList[1]
2
>>> myList[4]
5

If the index is greater than the lenght of the list, attempting such access will raise an exception. For example:

>>> mylist[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Knowing the size of a list: len()

Most of the time, you will use lists which are of differents sizes and you might not be able to foresee the length of the list before manipulating it. Since this is a common operation, Python has a built-in function len which takes a list as an argument and returns the length of the list.

>>> myList = [1, 2, 3, 4, 5]
>>> print(len(myList))
5

A list can have 1 single element:

>>> singleList = ["foo"]
>>> print(len(singleList))
1

Now, an example with a list of lists

>>> listOfLists = [1, [2, 3], [4, 5, 6]]
>>> print(len(listOfLists))
3 
>>> print(len(listOfLists[1]))
2 (the length of [2, 3])

Negative indexes

It is possible to use negative indexes. In such cases, Python will browse the list in reverse order (from the end to the beginning). For examplee myList[-1] will return the last element of myList. This notation is a welcomed shortcut in order to avoid too long instructions like myList[len(myList)-X] where X is the reverse index of some element one would need to use.

>>> myList = [1, 2, 3, 4, 5]
>>> print(myList[-1])
5

This is the equivalent with the following instruction (which is much longer)

>>> print(myList[len(myList)-1])
5 

Slicing

In some cases, it is useful to slice a list to get a fragment of it (either its beginning, its end, or some of the elements in the middle). This operation is very convenient to create new lists from existing ones, without having to fecth every single element. The syntax for slicing is the following: the name of the list, followed by two indexes separated by a colon, enclosed in square brackets. The first index is the one where to start the slice (the corresponding element is included in the result), the second index is the one where to end the slice (the corresponding element is not included in the result). The following examples show the different cases for slicing:

Creating a list with the 2 first elements:

>>> myList = [1, 2, 3, 4, 5]
>>> startList = myList[0:2]
>>> print(startList)
[1, 2]

Creating a third list with the last elements:

>>> myList = [1, 2, 3, 4, 5]
>>> endList = myList[3:5]
>>> print(endList)
[4, 5]

For the sake of completeness, here are some edge cases if you were wondering about errors:

>>> myList = [1, 2, 3, 4, 5]
>>> print(myList[0:85454])
[1,2,3,4,5]
>>> print(myList[2:1])
[] (since 2 is smaller than 1)

As mentioned earlier, negative indexes are a shortcut notation. There is also a shortcut notation for slicing so that it is more convenient to get X elements from the start of a given list or to get Y elements from its end (or even "get a sublist of all the elements except the first X elements" or "get a sublist of all the elements except the last Y elements"). The following examples show the different cases for slicing.

Creating a list with the 2 first elements, differently:

>>> myList = [1, 2, 3, 4, 5]
>>> startList = myList[:2]
>>> print(startList)
[1, 2]

Creating a list with the end, differently:

>>> myList = [1, 2, 3, 4, 5]
>>> endList = myList[-2:]
>>> print(endList)
[4, 5]

Creating a list without some elements from its beginning:

>>> myList = [1, 2, 3, 4, 5]
>>> headlessList = myList[2:]
>>> print(headlessList)
[3, 4, 5]

Creating a list without some elements from its end:

>>> myList = [1, 2, 3, 4, 5]
>>> queuelessList = myList[:-4]
>>> print(queuelessList)
[1]

Mutability

In Python, lists are mutable. That means that once you have defined a list, it is possible to change its content in following instructions. A list can be modified with direct access or via some operations or functions. These examples show some of these possible operations.

Changing the second element by setting it to 10:

>>> myList = [1, 2, 3, 4, 5]
>>> myList[1] = 10
>>> print(myList)
[1, 10, 3, 4, 5]

Concatenating two lists:

>>> listA = [1, 2, 3]
>>> listB = [2, 4, 6]
>>> listA = listA + listB
>>> print(listA)
[1, 2, 3, 2, 4, 6]

Using a built-in function to modify the list:

>>> myList = [1, "foo", "bar", 2]
>>> myList.reverse()
>>> print(myList)
[2, "bar", "foo", 1]

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, SphinxKnight, hbloomer, klez
 Last updated by: chrisdavidmills,