Python Programming/Lists

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.

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:

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 3NameError: name 'x'isnot 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.

You can initialize a list to a size, with an initial value for each element:

>>> zeros=[0]*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:

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:

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 inrange(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]

list=[1,2,3,4]list.pop()# Remove the last itemlist.pop(0)# Remove the first item , which is the item at index 0printlistlist=[1,2,3,4]dellist[1]# Remove the 2nd element; an alternative to list.pop(1)printlist

Removing an element by value:

list=["a","a","b"]list.remove("a")# Removes only the 1st occurrence of "a"printlist

Keeping only items in a list satisfying a condition, and thus removing the items that do not satisfy it:

There are some built-in functions for arithmetic aggregates over lists. These include minimum, maximum, and sum:

list=[1,2,3,4]printmax(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: