Python Notes

List Methods

In Python, lists have eleven methods. Most of them are designed to insert and delete items.

append() adds an element to the end of the list.

>>> lst = ['a', 45, 89, 'who']
>>> lst.append(67)
>>> lst
['a', 45, 89, 'who', 67]

clear() removes all items from the list.

>>> a = [1,2]
>>> a.clear()
>>> a
[]

copy() - makes a shallow copy of the list.

>>> a = [1, 2]
>>> b = a.copy()
>>> b.append(3)
>>> a
[1, 2]
>>> b
[1, 2, 3]
>>> c = [1, 2, [3, 4]]
>>> d = c.copy()
>>> d.append(5)
>>> c[2].append(6)
>>> c
[1, 2, [3, 4, 6]]
>>> d
[1, 2, [3, 4, 6], 5]

count() counts the number of times the argument is found in the list.

>>> a = [1, 0, 1, 1, 0]
>>> a.count(1)
3

extend() adds a sequence to the end of the list.

>>> b
[1, 2, 3]
>>> c = (9, 10)
>>> b.extend(c)
>>> b
[1, 2, 3, 9, 10]
>>> b.extend("abc")
>>> b
[1, 2, 3, 9, 10, 'a', 'b', 'c']
>>> b.extend([12, 19])
>>> b
[1, 2, 3, 9, 10, 'a', 'b', 'c', 12, 19]

index() returns the index of the specified element. If there are several such elements, only the first will be returned. If there are no such elements, an exception is generated. The second and third argument can specify a slice for the search.

>>> a = ['a', 'c', 'e', 'a', 'b']
>>> a.index('a')
0
>>> a.index('a', 2)
3
>>> a.index('a', 2, 4)
3
>>> a.index('a', 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'a' is not in list

insert() inserts the element in front of the specified index. An index is first, then an item.

>>> lst.insert(0,10)
>>> lst
[10, 'a', 45, 89, 'who', 67, 'a1', (1, 2, 3)]
>>> lst.insert(len(lst),10)
>>> lst
[10, 'a', 45, 89, 'who', 67, 'a1', (1, 2, 3), 10]
>>> lst.insert(3,10)
>>> lst
[10, 'a', 45, 10, 89, 'who', 67, 'a1', (1, 2, 3), 10]

pop() removes the item at the specified index and returns it. If the index is not specified, then deletes and returns the last element. The method generates exceptions if the list is empty or if the index is out of range.

>>> lst.pop()
10
>>> lst
['a', 45, 10, 89, 'who', 67, 'a1', (1, 2, 3)]
>>> lst.pop(1)
45
>>> lst
['a', 10, 89, 'who', 67, 'a1', (1, 2, 3)]
>>> lst.clear()
>>> lst.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop from empty list
>>> lst.append(10)
>>> lst.pop(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range

remove() removes the first object from the list, the value of which is equal to the argument. If there is no such element, an error occurs.

>>> lst.remove(10)
>>> lst
['a', 45, 10, 89, 'who', 67, 'a1', (1, 2, 3), 10]
>>> lst.remove(100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

reverse() reverses the list in place.

>>> lst
['a', 10, 89, 'who', 67, 'a1', (1, 2, 3), 10]
>>> lst.reverse()
>>> lst
[10, (1, 2, 3), 'a1', 67, 'who', 89, 10, 'a']

sort() sorts the list in place (the list changes, a new one is not returned).

>>> li = [4, 1, 9, 5]
>>> li.sort()
>>> li
[1, 4, 5, 9]
>>> print (li.sort())
None