Python Programming/Lists< Python Programming
A list in Python is an ordered group of items (or elements). It is a very general structure, and list elements don't have to be of the same type: you can put numbers, letters, strings and nested lists all on the same list.
Lists in Python at a glance:
list1 =  # A new empty list list2 = [1, 2, 3, "cat"] # A new non-empty list with mixed item types list1.append("cat") # Add a single member, at the end of the list list1.extend(["dog", "mouse"]) # Add several members if "cat" in list1: # Membership test list1.remove("cat") # Remove AKA delete #list1.remove("elephant") - throws an error for item in list1: # Iteration AKA for each item print item print "Item count:", len(list1) # Length AKA size AKA item count list3 = [6, 7, 8, 9] for i in range(0, len(list3)): # Read-write iteration AKA for each item list3[i] += 1 # Item access AKA element access by index last = list3[-1] # Last item nextToLast = list3[-2] # Next-to-last item isempty = len(list3) == 0 # Test for emptiness set1 = set(["cat", "dog"]) # Initialize set from a list list4 = list(set1) # Get a list from a set list5 = list4[:] # A shallow list copy list4equal5 = list4==list5 # True: same by value list4refEqual5 = list4 is list5 # False: not same by reference list6 = list4[:] del list6[:] # Clear AKA empty AKA erase list7 = [1, 2] + [2, 3, 4] # Concatenation print list1, list2, list3, list4, list5, list6, list7 print list4equal5, list4refEqual5 print list3[1:3], list3[1:], list3[:2] # Slices print max(list3 ), min(list3 ), sum(list3) # Aggregates
There are two different ways to make a list in Python. The first is through assignment ("statically"), the second is using list comprehensions ("actively").
To make a static list of items, write them between square brackets. For example:
[ 1,2,3,"This is a list",'c',Donkey("kong") ]
- The list contains items of different data types: integer, string, and Donkey class.
- Objects can be created 'on the fly' and added to lists. The last item is a new instance of Donkey class.
Creation of a new list whose members are constructed from non-literal expressions:
a = 2 b = 3 myList = [a+b, b+a, len(["a","b"])]
See also Tips and Tricks
Using list comprehension, you describe the process using which the list should be created. To do that, the list is broken into two pieces. The first is a picture of what each element will look like, and the second is what you do to get it.
For instance, let's say we have a list of words:
listOfWords = ["this","is","a","list","of","words"]
To take the first letter of each word and make a list out of it using list comprehension, we can do this:
>>> listOfWords = ["this","is","a","list","of","words"] >>> items = [ word for word in listOfWords ] >>> print items ['t', 'i', 'a', 'l', 'o', 'w']
List comprehension supports more than one for statement. It will evaluate the items in all of the objects sequentially and will loop over the shorter objects if one object is longer than the rest.
>>> item = [x+y for x in 'cat' for y in 'pot'] >>> print item ['cp', 'co', 'ct', 'ap', 'ao', 'at', 'tp', 'to', 'tt']
List comprehension supports an if statement, to only include members into the list that fulfill a certain condition:
>>> print [x+y for x in 'cat' for y in 'pot'] ['cp', 'co', 'ct', 'ap', 'ao', 'at', 'tp', 'to', 'tt'] >>> print [x+y for x in 'cat' for y in 'pot' if x != 't' and y != 'o' ] ['cp', 'ct', 'ap', 'at'] >>> print [x+y for x in 'cat' for y in 'pot' if x != 't' or y != 'o' ] ['cp', 'co', 'ct', 'ap', 'ao', 'at', 'tp', 'tt']
In version 2.x, Python's list comprehension does not define a scope. Any variables that are bound in an evaluation remain bound to whatever they were last bound to when the evaluation was completed. In version 3.x Python's list comprehension uses local variables:
>>> print x, y #Input to python version 2 r t #Output using python 2 >>> print x, y #Input to python version 3 NameError: name 'x' is not defined #Python 3 returns an error because x and y were not leaked
This is exactly the same as if the comprehension had been expanded into an explicitly-nested group of one or more 'for' statements and 0 or more 'if' statements.
List creation shortcutsEdit
You can initialize a list to a size, with an initial value for each element:
>>> zeros=*5 >>> print zeros [0, 0, 0, 0, 0]
This works for any data type:
>>> foos=['foo']*3 >>> print foos ['foo', 'foo', 'foo']
But there is a caveat. When building a new list by multiplying, Python copies each item by reference. This poses a problem for mutable items, for instance in a multidimensional array where each element is itself a list. You'd guess that the easy way to generate a two dimensional array would be:
listoflists=[ *4 ] *5
and this works, but probably doesn't do what you expect:
>>> listoflists=[ *4 ] *5 >>> print listoflists [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] >>> listoflists=1 >>> print listoflists [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]]
What's happening here is that Python is using the same reference to the inner list as the elements of the outer list. Another way of looking at this issue is to examine how Python sees the above definition:
>>> innerlist=*4 >>> listoflists=[innerlist]*5 >>> print listoflists [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] >>> innerlist=1 >>> print listoflists [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]]
Assuming the above effect is not what you intend, one way around this issue is to use list comprehensions:
>>> listoflists=[*4 for i in range(5)] >>> print listoflists [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] >>> listoflists=1 >>> print listoflists [[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
To find the length of a list use the built in len() method.
>>> len([1,2,3]) 3 >>> a = [1,2,3,4] >>> len( a ) 4
Lists can be combined in several ways. The easiest is just to 'add' them. For instance:
>>> [1,2] + [3,4] [1, 2, 3, 4]
Another way to combine lists is with extend. If you need to combine lists inside of a lambda, extend is the way to go.
>>> a = [1,2,3] >>> b = [4,5,6] >>> a.extend(b) >>> print a [1, 2, 3, 4, 5, 6]
The other way to append a value to a list is to use append. For example:
>>> p=[1,2] >>> p.append([3,4]) >>> p [1, 2, [3, 4]] >>> # or >>> print p [1, 2, [3, 4]]
However, [3,4] is an element of the list, and not part of the list. append always adds one element only to the end of a list. So if the intention was to concatenate two lists, always use extend.
Getting pieces of lists (slices)Edit
Like strings, lists can be indexed and sliced:
>>> list = [2, 4, "usurp", 9.0, "n"] >>> list 'usurp' >>> list[3:] [9.0, 'n']
Much like the slice of a string is a substring, the slice of a list is a list. However, lists differ from strings in that we can assign new values to the items in a list:
>>> list = 17 >>> list [2, 17, 'usurp', 9.0, 'n']
We can assign new values to slices of the lists, which don't even have to be the same length:
>>> list[1:4] = ["opportunistic", "elk"] >>> list [2, 'opportunistic', 'elk', 'n']
It's even possible to append items onto the start of lists by assigning to an empty slice:
>>> list[:0] = [3.14, 2.71] >>> list [3.14, 2.71, 2, 'opportunistic', 'elk', 'n']
Similarly, you can append to the end of the list by specifying an empty slice after the end:
>>> list[len(list):] = ['four', 'score'] >>> list [3.14, 2.71, 2, 'opportunistic', 'elk', 'n', 'four', 'score']
You can also completely change the contents of a list:
>>> list[:] = ['new', 'list', 'contents'] >>> list ['new', 'list', 'contents']
The right-hand side of a list assignment statement can be any iterable type:
>>> list[:2] = ('element',('t',),) >>> list ['element', ('t',), , 'contents']
With slicing you can create copy of list since slice returns a new list:
>>> original = [1, 'element', ] >>> list_copy = original[:] >>> list_copy [1, 'element', ] >>> list_copy.append('new element') >>> list_copy [1, 'element', , 'new element'] >>> original [1, 'element', ]
Note, however, that this is a shallow copy and contains references to elements from the original list, so be careful with mutable types:
>>> list_copy.append('something') >>> original [1, 'element', ['something']]
It is also possible to get non-continuous parts of an array. If one wanted to get every n-th occurrence of a list, one would use the :: operator. The syntax is a:b:n where a and b are the start and end of the slice to be operated upon.
>>> list = [i for i in range(10) ] >>> list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list[::2] [0, 2, 4, 6, 8] >>> list[1:7:2] [1, 3, 5]
Lists can be compared for equality.
>>> [1,2] == [1,2] True >>> [1,2] == [3,4] False
Lists can be compared using a less-than operator, which uses lexicographical order:
>>> [1,2] < [2,1] True >>> [2,2] < [2,1] False >>> ["a","b"] < ["b","a"] True
Sorting lists is easy with a sort method.
>>> list = [2, 3, 1, 'a', 'b'] >>> list.sort() >>> list [1, 2, 3, 'a', 'b']
Note that the list is sorted in place, and the sort() method returns None to emphasize this side effect.
If you use Python 2.4 or higher there are some more sort parameters:
cmp : method to be used for sorting key : function to be executed with key element. List is sorted by return-value of the function reverse : sort(reverse=True) or sort(reverse=False)
Python also includes a sorted() function.
>>> list = [5, 2, 3, 'q', 'p'] >>> sorted(list) [2, 3, 5, 'p', 'q'] >>> list [5, 2, 3, 'q', 'p']
Note that unlike the sort() method, sorted(list) does not sort the list in place, but instead returns the sorted list. The sorted() function, like the sort() method also accepts the reverse parameter.
Iteration over lists:
Read-only iteration over a list, AKA for each element of the list:
list1 = [1, 2, 3, 4] for item in list1: print item
Writable iteration over a list:
list1 = [1, 2, 3, 4] for i in range(0, len(list1)): list1[i]+=1 # Modify the item at an index as you see fit print list
From a number to a number with a step:
for i in range(1, 13+1, 3): # For i=1 to 13 step 3 print i for i in range(10, 5-1, -1): # For i=10 to 5 step -1 print i
For each element of a list satisfying a condition (filtering):
for item in list: if not condition(item): continue print item
See also Python Programming/Loops#For_Loops.
Removing aka deleting an item at an index (see also #pop(i)):
list = [1, 2, 3, 4] list.pop() # Remove the last item list.pop(0) # Remove the first item , which is the item at index 0 print list list = [1, 2, 3, 4] del list # Remove the 2nd element; an alternative to list.pop(1) print list
Removing an element by value:
list = ["a", "a", "b"] list.remove("a") # Removes only the 1st occurrence of "a" print list
Keeping only items in a list satisfying a condition, and thus removing the items that do not satisfy it:
list = [1, 2, 3, 4] newlist = [item for item in list if item >2] print newlist
This uses a list comprehension.
There are some built-in functions for arithmetic aggregates over lists. These include minimum, maximum, and sum:
list = [1, 2, 3, 4] print max(list), min(list), sum(list) average = sum(list) / float(len(list)) # Provided the list is non-empty # The float above ensures the division is a float one rather than integer one. print average
The max and min functions also apply to lists of strings, returning maximum and minimum with respect to alphabetical order:
list = ["aa", "ab"] print max(list), min(list) # Prints "ab aa"
Copying AKA cloning of lists:
Making a shallow copy:
list1= [1, 'element'] list2 = list1[:] # Copy using "[:]" list2 = 2 # Only affects list2, not list1 print list1 # Displays 1 # By contrast list1 = [1, 'element'] list2 = list1 list2 = 2 # Modifies the original list print list1 # Displays 2
The above does not make a deep copy, which has the following consequence:
list1 = [1, [2, 3]] # Notice the second item being a nested list list2 = list1[:] # A shallow copy list2 = 4 # Modifies the 2nd item of list1 as well print list1 # Displays 4 rather than 2
Making a deep copy:
import copy list1 = [1, [2, 3]] # Notice the second item being a nested list list2 = copy.deepcopy(list1) # A deep copy list2 = 4 # Leaves the 2nd item of list1 unmodified print list1 # Displays 2
See also #Continuous slices.
- 8.17. copy — Shallow and deep copy operations at docs.python.org
Clearing a list:
del list1[:] # Clear a list list1 =  # Not really clear but rather assign to a new empty list
Clearing using a proper approach makes a difference when the list is passed as an argument:
def workingClear(ilist): del ilist[:] def brokenClear(ilist): ilist =  # Lets ilist point to a new list, losing the reference to the argument list list1=[1, 2]; workingClear(list1); print list1 list1=[1, 2]; brokenClear(list1); print list1
Keywords: emptying a list, erasing a list, clear a list, empty a list, erase a list.
Removing duplicate itemsEdit
Removing duplicate items from a list (keeping only unique items) can be achieved as follows.
If each item in the list is hashable:
list1 = [1, 4, 4, 5, 3, 2, 3, 2, 1] visited = set() i = 0 while i < len(list1): if list1[i] in visited: list1.pop(i) continue visited.add(list1[i]) i+=1
If some items are not hashable, the set of visited items can be kept in a list:
list1 = [1, 4, 4, ["a", "b"], 5, ["a", "b"], 3, 2, 3, 2, 1] visited =  i = 0 while i < len(list1): if list1[i] in visited: list1.pop(i) continue visited.append(list1[i]) i+=1
If each item in the list is hashable and preserving element order does not matter:
list1 = [1, 4, 4, 5, 3, 2, 3, 2, 1] list1 = list(set(list1))
Add item x onto the end of the list.
>>> list = [1, 2, 3] >>> list.append(4) >>> list [1, 2, 3, 4]
Remove the item in the list at the index i and return it. If i is not given, remove the the last item in the list and return it.
>>> list = [1, 2, 3, 4] >>> a = list.pop(0) >>> list [2, 3, 4] >>> a 1 >>> b = list.pop() >>>list [2, 3] >>> b 4
The operator 'in' is used for two purposes; either to iterate over every item in a list in a for loop, or to check if a value is in a list returning true or false.
>>> list = [1, 2, 3, 4] >>> if 3 in list: >>> .... >>> l = [0, 1, 2, 3, 4] >>> 3 in l True >>> 18 in l False >>>for x in l: >>> print x 0 1 2 3 4
In a modern version of Python [which one?], there is a class called 'list'. You can make your own subclass of it, and determine list behaviour which is different from the default standard.
- Use a list comprehension to construct the list ['ab', 'ac', 'ad', 'bb', 'bc', 'bd'].
- Use a slice on the above list to construct the list ['ab', 'ad', 'bc'].
- Use a list comprehension to construct the list ['1a', '2a', '3a', '4a'].
- Simultaneously remove the element '2a' from the above list and print it.
- Copy the above list and add '2a' back into the list such that the original is still missing it.
- Use a list comprehension to construct the list ['abe', 'abf', 'ace', 'acf', 'ade', 'adf', 'bbe', 'bbf', 'bce', 'bcf', 'bde', 'bdf']