You are a guest. Restricted access. Read more


A list a data container which keeps values (numbers, strings, objects). The values should have the same type, which can be checked with the command type(value). Values in a list are numbered, starting from zero - the first one is numbered zero, the second 1, the third 2, etc. Let us make a list of numbers:

print L

The list can be written as a list of comma-separated values (items) between square brackets.

In fact, one can first specify an empty list and then add the numbers using the append method.

L=[]          # define an empty list
L.append(1)   # append value 1
print L

We can calculate the length of this list using the function len(). Also you can access any element of the list using it's index:

print len(L)  # print the length of this list
print L[2]    # print the value at the position 2

Often we need to create a large lists, in which case typing a long list become tedious. In this case use the statement range() It takes at least 2 parameters: first the number at which the list it returns should start, and second, the number up to which the list should go.

print L

range() takes an optimal third parameter that specifies the step between each number in the list:

print L

Looping Over Lists

The for-in statement makes it easy to loop over the items in a list:

for item in L:
     print item

If you need both the index and the item, use the enumerate function:

for index, item in enumerate(L):
        print index, item

If you need only the index, use range and len:

for index in range(len(L)):
             print index

Python provides various shortcuts for common list operations. For example, if a list contains numbers, the built-in sum function gives you the sum:

print sum(L)

If a list contains strings, you can combine the string into a single long string using the join string method:

s = ''.join(L)
print s

Python also provides built-in operations to search for items, and to sort the list.

Modifying Lists

The list type also allows you to assign to individual items or slices, and to delete them.

L[1] = "test"
L[1:2] =["1","2"]
print L

Making a copy

To create a separate list, you can use slicing or the list function to quickly create a copy:

L = [1,2,3,4,5]
M = L[:] # create a copy
print M
print M

The append method adds a single item to the end of the list, the extend method adds items from another list (or any sequence) to the end, and insert inserts an item at a given index, and move the remaining items to the right.

To insert items from another list or sequence at some other location, use slicing syntax:

You can also remove items. The del statement can be used to remove an individual item, or to remove all items identified by a slice. The pop method removes an individual item and returns it, while remove searches for an item, and removes the first matching item from the list.

del L[1]
del L[5:8]
print L

The list type allows you to quickly reverse the order of the list, L.reverse()

python/collections/lists.txt · Last modified: 2013/04/14 16:52 (external edit)
Back to top
CC Attribution-Share Alike 3.0 Unported = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0