The list data type has some more methods. Here are all of the methods of lists objects:
insert(i, x)
a.insert(0, x)
inserts at
the front of the list, and a.insert(len(a), x)
is equivalent to
a.append(x)
.
append(x)
a.insert(len(a), x)
.
index(x)
x
.
It is an error if there is no such item.
remove(x)
x
.
It is an error if there is no such item.
sort()
reverse()
count(x)
x
appears in the list.
An example that uses all list methods:
>>> a = [66.6, 333, 333, 1, 1234.5] >>> print a.count(333), a.count(66.6), a.count('x') 2 1 0 >>> a.insert(2, -1) >>> a.append(333) >>> a [66.6, 333, -1, 333, 1, 1234.5, 333] >>> a.index(333) 1 >>> a.remove(333) >>> a [66.6, -1, 333, 1, 1234.5, 333] >>> a.reverse() >>> a [333, 1234.5, 1, 333, -1, 66.6] >>> a.sort() >>> a [-1, 1, 66.6, 333, 333, 1234.5] >>>