append (x) Appends x to object
count
(x)
Number of occurrences of object x
del l[i:j:k]
[i:j:k]
Deletes elements with index values i to j – 1
extend
(s)
Appends all elements of s to object
index
(x[, i[, j]])
First index of x between elements i and j – 1
insert
(i, x)++
Inserts x at/before index i
remove
(i)
Removes element with index i
pop
(i)
Removes element with index i and return it
reverse
()
Reverses all items in place
sort
([cmp[, key[, reverse]]])
Sorts all items in place
Excursion: Control Structures
Although a topic in itself, control structures like for loops are maybe best introduced in
Python based on list objects. This is due to the fact that looping in general takes place
over list objects, which is quite different to what is often the standard in other languages.
Take the following example. The for loop loops over the elements of the list object l
with index values 2 to 4 and prints the square of the respective elements. Note the
importance of the indentation (whitespace) in the second line:
In [ 52 ]: for element in l[ 2 : 5 ]:
print element ** 2
Out[52]: 6.25
1.0
2.25
This provides a really high degree of flexibility in comparison to the typical counter-based
looping. Counter-based looping is also an option with Python, but is accomplished based
on the (standard) list object range:
In [ 53 ]: r = range( 0 , 8 , 1 ) # start, end, step width
r
Out[53]: [0, 1, 2, 3, 4, 5, 6, 7]
In [ 54 ]: type(r)
Out[54]: list
For comparison, the same loop is implemented using range as follows: