 Prev: List of Lists       |       Next: -

List functions and methods¶

Functions¶

• Python has a number of built-in functions that are always available.
• We present here four major functions relevant to lists (speaking more generally these are functions that can be applied to any iterable object)

len(list)¶

Returns the 'length' of a list, that is, the number of items a list contains. Calling len() is valid for all sequence objects (string, tuple, etc.) or collections (dictionary, etc.)

In :
alist = list('sequence')
len(alist)
Out:
8

max(list), min(list)¶

Return the item having the largest and smallest value in the list.

In :
import random
alist = [random.randint(1,100) for i in range(10)]
max(alist), min(alist)
Out:
(96, 12)

sorted(list)¶

Returns a new sorted list from the list passed as argument

In :
alist = [random.randint(1,100) for i in range(10)]
print(alist)
print(sorted(alist))
print(sorted(alist)[::-1])
[49, 70, 2, 41, 58, 97, 51, 82, 57, 77]
[2, 41, 49, 51, 57, 58, 70, 77, 82, 97]
[97, 82, 77, 70, 58, 57, 51, 49, 41, 2]

sum(list)¶

Returns the sume of numerical items in a list

In :
alist = [random.randint(1,100) for i in range(2)]
print(alist)
sum(alist)
[32, 33]
Out:
65
• See all Python built-in functions here Methods¶

List methods enable various forms of list item processing. Most important of them are:

list.append(x)¶

Adds (appends) x at the end of the list

In :
alist = []
for i in range(3):
alist.append(i)
alist
Out:
[0, 1, 2]

list.extend(L)¶

Adds the L iterable at the end of the list

In :
alist = [1,2,3]
blist = ['a','b','c']
blist.extend(alist)
blist
Out:
['a', 'b', 'c', 1, 2, 3]

list.insert(i,x)¶

Inserts x in place before that with index i

In :
alist = ['A','B','D']
alist.insert(2,'C')
alist
Out:
['A', 'B', 'C', 'D']

list.remove(x)¶

Removes x from list (from the place that x first appears)

In :
alist = ['A','B','C','D']
alist.remove('B')
alist
Out:
['A', 'C', 'D']

list.pop(i)¶

Removes and returns the item in position with index i. If no position i is defined then the last item in the list is removed

In :
alist = ['A','B','C','D']
x = alist.pop(0)
print(x, alist)

y = alist.pop()
print(y, alist)
A ['B', 'C', 'D']
D ['B', 'C']

list.clear()¶

Removes all list items. The list remains empty.

In :
alist = ['A','B','C','D']
alist.clear()
alist
Out:
[]

list.index(x)¶

Returns the index of the position wher x is found (for the first time).

In :
alist = ['A','B','C','D']
alist.index('B')
Out:
1

list.count(x)¶

Returns a numeric indicating how many times the 'x' is found in the list

In :
alist = ['A','B','C','D','A','E','F','G']
alist.count('A')
Out:
2

list.sort()¶

Sort the list ('in place': it does NOT return a new list).

In :
import random
alist = [random.randint(1,100) for i in range(10)]
alist.sort()
alist
Out:
[8, 12, 14, 16, 16, 43, 62, 77, 86, 95]

list.reverse()¶

Reverses the list ('in place': it does NOT return a new list).

In :
import random
alist = [random.randint(1,100) for i in range(10)]
alist.sort()
print(alist)
alist.reverse()
print(alist)
[3, 8, 32, 44, 49, 53, 56, 85, 87, 89]
[89, 87, 85, 56, 53, 49, 44, 32, 8, 3]

list.copy()¶

Returns a copy of the list (similar to [:] notation)

In :
import random
alist = [random.randint(1,100) for i in range(10)]
blist = alist.copy()
print(alist)
print(blist)
[85, 26, 4, 17, 21, 65, 8, 40, 67, 98]
[85, 26, 4, 17, 21, 65, 8, 40, 67, 98]

'Shallow' vs. 'Deep' copy¶

• When copying a container object (for example a list) that contains other mutable objects (for example, other lists) then the question arises whether the copy is 'shallow' or 'deep'
• 'Shallow' means that ONLY the container object is copied and pointers are used to set bindings with already existing contained objects. Practically this results to a situation where any change in the copied object is reflected also on the original.
In :
listorig = [100, ['A','B']]
listcopy = listorig.copy()
print(listorig)
print(listcopy)

# Now we change the copied list (the contained list)
listcopy = 'XX'
print(listcopy)
print(listorig)     # Original list appears also changed
[100, ['A', 'B']]
[100, ['A', 'B']]
[100, ['XX', 'B']]
[100, ['XX', 'B']]
• 'Deep' means that BOTH the container and nested objects are copied. Therefore, changes in the copied object do NOT reflect on the original. To perform a 'deep copy' you need to import the copy module and use the deepcopy method as in the example below.
In :
import copy
listorig = [100, ['A','B']]
listcopy = copy.deepcopy(listorig)
print(listorig)
print(listcopy)

# Now we change the copied list (the contained list)
listcopy = 'XX'
print(listcopy)
print(listorig)     # The original list appears now intact
[100, ['A', 'B']]
[100, ['A', 'B']]
[100, ['XX', 'B']]
[100, ['A', 'B']] 