Prev: List of Lists | Next: -
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.)
alist = list('sequence')
len(alist)
Return the item having the largest and smallest value in the list.
import random
alist = [random.randint(1,100) for i in range(10)]
max(alist), min(alist)
Returns a new sorted list from the list passed as argument
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
alist = [random.randint(1,100) for i in range(2)]
print(alist)
sum(alist)
List methods enable various forms of list item processing. Most important of them are:
Adds (appends) x at the end of the list
alist = []
for i in range(3):
alist.append(i)
alist
Adds the L iterable at the end of the list
alist = [1,2,3]
blist = ['a','b','c']
blist.extend(alist)
blist
Inserts x in place before that with index i
alist = ['A','B','D']
alist.insert(2,'C')
alist
Removes x from list (from the place that x first appears)
alist = ['A','B','C','D']
alist.remove('B')
alist
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
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.
alist = ['A','B','C','D']
alist.clear()
alist
Returns the index of the position wher x is found (for the first time).
alist = ['A','B','C','D']
alist.index('B')
Returns a numeric indicating how many times the 'x' is found in the list
alist = ['A','B','C','D','A','E','F','G']
alist.count('A')
Sort the list ('in place': it does NOT return a new list).
import random
alist = [random.randint(1,100) for i in range(10)]
alist.sort()
alist
Reverses the list ('in place': it does NOT return a new list).
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)
import random
alist = [random.randint(1,100) for i in range(10)]
blist = alist.copy()
print(alist)
print(blist)
- '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.
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 to import the copy module and use the deepcopy method as in the example below.
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