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 [1]:
alist = list('sequence')
len(alist)

Out[1]:
8

#### max(list), min(list)¶

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

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

Out[6]:
(96, 12)

#### sorted(list)¶

Returns a new sorted list from the list passed as argument

In [30]:
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 [27]:
alist = [random.randint(1,100) for i in range(2)]
print(alist)
sum(alist)

[32, 33]

Out[27]:
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 [34]:
alist = []
for i in range(3):
alist.append(i)
alist

Out[34]:
[0, 1, 2]

#### list.extend(L)¶

Adds the L iterable at the end of the list

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

Out[35]:
['a', 'b', 'c', 1, 2, 3]

#### list.insert(i,x)¶

Inserts x in place before that with index i

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

Out[39]:
['A', 'B', 'C', 'D']

#### list.remove(x)¶

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

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

Out[40]:
['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 [44]:
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 [47]:
alist = ['A','B','C','D']
alist.clear()
alist

Out[47]:
[]

#### list.index(x)¶

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

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

Out[48]:
1

#### list.count(x)¶

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

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

Out[50]:
2

#### list.sort()¶

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

In [60]:
import random
alist = [random.randint(1,100) for i in range(10)]
alist.sort()
alist

Out[60]:
[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 [68]:
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 [69]:
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 [3]:
listorig = [100, ['A','B']]
listcopy = listorig.copy()
print(listorig)
print(listcopy)

# Now we change the copied list (the contained list)
listcopy[1][0] = '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 [5]:
import copy
listorig = [100, ['A','B']]
listcopy = copy.deepcopy(listorig)
print(listorig)
print(listcopy)

# Now we change the copied list (the contained list)
listcopy[1][0] = 'XX'
print(listcopy)
print(listorig)     # The original list appears now intact

[100, ['A', 'B']]
[100, ['A', 'B']]
[100, ['XX', 'B']]
[100, ['A', 'B']]


. Free learning material
. See full copyright and disclaimer notice