Examples of elementary operations on lists in python
Table of contents
Create a list
Create an empty list
>>> l = []>>> type(myfirstlist)<type 'list'>
Create a list with 3 elements
>>> l = ['a','b','c']
Note: in a list the elements can have different typrs
>>> l = ['hello',1,2]
Get the number of elements in a list
To get the number of elements in a list a solution is to use len()
>>> l = ['Pierre', 'Paul', 'Mathieu']>>> len(l)3
here the list has 3 elements
Extract element from a list
To get an element stored in a list:
>>> l = ['Pierre', 'Paul', 'Mathieu']>>> l[0]'Pierre'>>> l[1]'Paul'>>> l[2]'Mathieu'
Create a loop
>>> l = ['Pierre', 'Paul', 'Mathieu']>>> for i in l:... print( i )...PierrePaulMathieu
Get the frequency of an element
To get a count of an element a solution is to use count():
>>> l = ['Pierre', 'Paul', 'Mathieu','Paul']>>> l.count('Pierre')1>>> l.count('Paul')2
here 'Paul' appeared two times in the list
Get element index
To get element index a solution is to use index()
>>> l = ['Pierre', 'Paul', 'Mathieu']>>> l.index('Paul')1
If the element does not exist it returns an error
>>> l.index('Ben')Traceback (most recent call last):File "<stdin>", line 1, in <module>ValueError: 'Ben' is not in list
Add an element in a list
To add an element in a list a solution is to use append()
>>> l = ['Pierre', 'Paul', 'Mathieu']>>> l.append('toto')>>> l['Pierre', 'Paul', 'Mathieu', 'toto']
Note: append() add the new element at the end of the list.
To add an element in first position a solution is to do for example:
>>> l = ['Pierre', 'Paul', 'Mathieu']>>> l = ['Ben'] + l>>> l['Ben', 'Pierre', 'Paul', 'Mathieu']
Another solution is to use insert() to specify the position in the list of a new element:
>>> l = ['Pierre', 'Paul', 'Mathieu']>>> l.insert(1, 'toto')>>> l['Pierre', 'toto', 'Paul', 'Mathieu']
Merge two lists
Two lists can be merged using extend():
>>> l1 = ['Pierre', 'Paul', 'Mathieu']>>> l2 = ['Toto', 'George']>>> l1.extend(l2)>>> l1['Pierre', 'Paul', 'Mathieu', 'Toto', 'George']
Another solution is to use the + operator
>>> l1 = ['Pierre', 'Paul', 'Mathieu']>>> l2 = ['Toto', 'George']>>> l3 = l1 + l2>>> l3['Pierre', 'Paul', 'Mathieu', 'Toto', 'George']
Remove an element
To remove an element a solution is to use remove():
>>> l = ['Pierre', 'Paul', 'Mathieu']>>> l.remove('Paul')>>> l['Pierre', 'Mathieu']>>> l.remove('Ben')Traceback (most recent call last):File "<stdin>", line 1, in <module>ValueError: list.remove(x): x not in list
Another solution is to use pop()
>>> l = ['Pierre', 'Paul', 'Mathieu','Paul']>>> l.pop(3)'Paul'>>> l['Pierre', 'Paul', 'Mathieu']
Slice a list
Example of how to slice a list
>>> l1 = ['Pierre', 'Paul', 'Mathieu','Paul']>>> l2 = l1[0:2]>>> l2['Pierre', 'Paul']>>> l3 = l1[1:3]>>> l3['Paul', 'Mathieu']
Sort a list
To sort a list a solution is to use sort()
>>> l = [4,3,2,1]>>> l.sort()>>> l[1, 2, 3, 4]
Reverse a list
To reverse a list a solution is to use reverse()
>>> l1 = ['Pierre', 'Paul', 'Mathieu','Paul']>>> l1.reverse()>>> l1['Paul', 'Mathieu', 'Paul', 'Pierre']
Check if an element exist
>>> l = ['Pierre', 'Paul', 'Mathieu','Paul']>>> 'George' in lFalse>>> 'Pierre' in lTrue
Check if an element is not in a list
>>> l = ['Pierre', 'Paul', 'Mathieu','Paul']>>> 'George' not in lTrue>>> 'Pierre' not in lFalse
