Functions on list
In the previous section we have seen how to use some operators on the lists, but the major work on them will have to be done using appropriate functions. Python provides a good set of functions that allow you to manipulate the lists and their content efficiently.
Add an item to a list
For example, the most common operation that must be performed once a list has been defined will be to add further elements during the execution of the code. To do this we will use the append () method.
Note: In Python, the term “method” is used to indicate a function that works on an object. So we will have object.method()
>>> lista = [0,1,2,3,4] >>> lista.append(5) >>> lista [0,1,2,3,4,5]
Insert an item to a list
When we want to insert a new element in a list, but not at the end, but in a certain intermediate position, we will use the insert () method. This method requires two parameters, the index that defines the position in which to insert the element and the value to be inserted.
>>> lista = [0,1,2,3,4] >>> lista.insert(3,0) >>> lista [0,1,2,0,3,4]
Find an item within a list
It can often be very useful to know the position of an element in a list, and to do so you use the index () method. In fact, this method will search for a value within a list by scanning the indices in ascending order, then once found, it will return the value of the corresponding index. If there are multiple elements with the same value, this method will return the one with the lowest index (the first one found). In the event that the value is not present in the list, the method throws a ValueError type error.
>>> lista = [0,1,2,3,4,3] >>> lista.index(3) 4 >>> lista.index(6) Traceback (most recent call last): File "<pyshell#54>", line 1, in lista.index(6) ValueError: 6 is not in list
Find the maximum and minimum value in a list
Sometimes it may be useful to find the maximum and minimum value in a list. This can be done by applying the max () and min () functions to a list. These two functions do not belong to list objects, and therefore they are not methods, but they can work on list objects as long as they are iterable objects (in simple words they contain numerical values). It is sufficient to pass the list as an argument of the two functions in order to obtain the maximum value and the minimum value of a list as the value returned.
>>> lista = [0,1,2,3,4] >>> max(lista) 4 >>> min(lista) 0
Sort the elements of a list in ascending order
Another very useful operation is to sort the items within a list. This can be done by applying the sort () method to the list. This is a method and therefore can also work on string lists.
>>> lista = [2,1,0,4,3] >>> lista.sort() >>> lista [0,1,2,3,4] >>> words = ["casa","mela","albero"] >>> words.sort() >>> words ["albero","casa","mela"]
Remove an item from the list
Another very important operation is to remove an item from the list. To do this, use the remove () method.
>>> lista = [0,1,2,3,4] >>> lista.remove(3) >>> lista [0,1,2,4]
Count the items in a list
Sometimes it may be useful to know how many elements are present in a list. To do this we use the len () function which requires the list passed as an argument.
>>> lista = [0,1,2,3,4] >>> len(lista) 5
Furthermore, it is possible to know how many times a certain value is contained within a list. For this we use the count () method which takes as its argument the value to look for and returns the number of times this value is present.
>>> lista = [0,1,2,1,4] >>> lista.count(1) 2
Invert the sequence of elements in a list
Finally there is a method that reverses the order of the elements contained in a list, this method is called reverse ().
>>> lista = [0,1,2,3,4] >>> lista.reverse() >>> lista [4,3,2,1,0]