Learn Python Tutorial - Lists



Python Basics

(Learn Python Programming in 20 Minutes)




3 Python commands



3.8 Lists

As already mentioned, a list is a finite sequence of items, and one could use the range function to create lists of integers. 
In Python, lists are not required to be homogeneous, i.e., the items could be of different types. For example,

a = [2, "Jack", 45, "23 Wentworth Ave"]


is a perfectly valid list consisting of two integers and two strings. One can refer to the entire list using the identifier a or to the i-th item in the list using a[i].


>>> a = [2, "Jack", 45, "23 Wentworth Ave"] 
>>> a [2, ’Jack’, 45, ’23 Wentworth Ave’] 
>>> a[0] 

>>> a[1] 
’Jack’ 
>>> a[2] 
45 
>>> a[3] 
’23 Wentworth Ave’

Note that the numbering of list items always begins at 0 in Python. So the four items in the above list are indexed by the numbers 0, 1, 2, 3. 
List items may be assigned a new value; this of course changes the list. For example, with a as above:

>>> a 
[2, ’Jack’, 45, ’23 Wentworth Ave’] 
>>> a[0] = 2002 
>>> a 
[2002, ’Jack’, 45, ’23 Wentworth Ave’]

Of course, the entire list may be assigned a new value, which does not have to be a list. When this happens, the previous value is lost:

>>> a 

[2002, ’Jack’, 45, ’23 Wentworth Ave’] 
>>> a = ’gobbletygook’ 
>>> a 
’gobbletygook’ 

 

3.8.1 Length of a list; empty list


Every list has a length, the number of items in the list, obtained using the len function:

>>> x = [9, 4, 900, -45] 

>>> len(x) 
4

Of special importance is the empty list of length 0. This is created as follows:


>>> x = [] 

>>> len(x) 
0


3.8.2 Sublists (slicing)

Sublists are obtained by slicing, which works analogously to the range function discussed before. If x is an existing list, then x[start:end] is the sublist consisting of all items in the original list at index positions i such that 

start ≤ i < end. 
Of course, we must remember that indexing items always starts at 0 in Python. For example,

 >>> x=range(0,20,2) 

>>> x 
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 
>>> x[2:5] 
[4, 6, 8] 
>>> x[0:5] 
[0, 2, 4, 6, 8]

When taking a slice, either parameter start or end may be omitted: if start is omitted then the slice consists of all items up to, but not including, the one at index position end, similarly, if end is omitted the slice consists of all items starting with the one at position start. For instance, with the list x as defined above we have


>>> x[:5] 

[0, 2, 4, 6, 8] 
>>> x[2:] 
[4, 6, 8, 10, 12, 14, 16, 18]

In this case, x[:5] is equivalent to x[0:5] and x[2:] is equivalent to x[2:len(x)]

There is an optional third parameter in a slice, which if present represents an increment, just as in the range function. For example,

>>> list = range(20) 

>>> list 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] 
>>> list[0:16:2] 
[0, 2, 4, 6, 8, 10, 12, 14] 
>>> list[0:15:2] 
[0, 2, 4, 6, 8, 10, 12, 14]

Notice that one may cleverly use a negative increment to effectively reverse a list, as in:


>>> list[18::-1] 

[17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In general, the slice x[len(x)::-1] reverses any existing list x.



3.8.3 Joining two lists

Two existing lists may be concatenated together to make a longer list, using the + operator:

>>> [2,3,6,10] + [4,0,0,5,0] 

[2, 3, 6, 10, 4, 0, 0, 5, 0]


3.8.4 List methods

If x is the name of an existing list, we can append an item item to the end of the list using

x.append(item)


For example,


>>> x = [3, 6, 8, 9] 

>>> x.append(999) 
>>> x [3, 6, 8, 9, 999]

A similar method is called insert, which allows an element to be inserted in the list at a specified position:

>>> x = [’a’, ’c’, ’3’, ’d’, ’7’] 

>>> x.insert(0,100) 
>>> x 
[100, ’a’, ’c’, ’3’, ’d’, ’7’] 
>>> x.insert(3,’junk’) 
>>> x 
[100, ’a’, ’c’, ’junk’, ’3’, ’d’, ’7’]
 
One can also delete the first occurrence of some item in the list (if possible) using remove as follows:


>>> x.remove(’a’) 

>>> x 
[100, ’c’, ’junk’, ’3’, ’d’, ’7’]

To delete the item at index position i use x.pop(i), as in:


>>> x.pop(0) 100 

>>> x 
[’c’, ’junk’, ’3’, ’d’, ’7’]

Notice that pop not only changes the list, but it also returns the item that was deleted. Also, by default x.pop() pops off the last item:


>>> x.pop() ’7’ 

>>> x 
[’c’, ’junk’, ’3’, ’d’]

Many more methods exist for manipulating lists; consult the Python Tutorial [1] or Python Library Reference [2] for more details.
 




 


No comments:

Post a Comment

Your feedback is highly appreciated and will help us to improve our content.