Prev: List of Lists | Next: -

- 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)

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]:

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]:

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])
```

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)
```

Out[27]:

- See all Python built-in functions here

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

Adds (appends) x at the end of the list

In [34]:

```
alist = []
for i in range(3):
alist.append(i)
alist
```

Out[34]:

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]:

Inserts x in place before that with index i

In [39]:

```
alist = ['A','B','D']
alist.insert(2,'C')
alist
```

Out[39]:

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

In [40]:

```
alist = ['A','B','C','D']
alist.remove('B')
alist
```

Out[40]:

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)
```

Removes all list items. The list remains empty.

In [47]:

```
alist = ['A','B','C','D']
alist.clear()
alist
```

Out[47]:

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]:

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]:

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]:

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)
```

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)
```

- 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
```

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 toimport the copy moduleand use thedeepcopymethod 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
```

. Free learning material

. See full copyright and disclaimer notice