1 - Create a list in python
Creating a list in Python language is a trivial operation. To create a list, nothing is easier:Example
>>> list = []
You can see the contents of the list by calling it like this:
>>> list
2 - Add a value to a python list
You can add the values you want when creating the python list:Example
>>> list = [1,2,3,4,7,11]
>>> list
[1, 2, 3,4,7,11]
Or add them after creating the list with the append method (which means "add" in English):Example
>>> list = []
>>> list
[]
>>> list.append (1)
>>> list
[1]
>>> list.append ("a")
>>> list
[1, 'a']
We see that it is possible to mix in the same list variables of different types.
3 - Display an item from a list
To read a list, we can ask to see the index of the value that interests us:
Example
>>> list = ["a", "b", "c"]
>>> list [0]
'a'
>>> list [2]
'c'
The first item always starts with the index 0. To read the first item we use the value 0, the second we use the value 1, etc. It is also possible to modify a value with its index :
Example
>>> list = ["a", "e", "g"]
>>> list [0]
'a'
>>> list [2]
'g'
>>> list [2] = "k"
>>> list
['a', 'e', 'k']
4 - Delete an entry from a python list
4-1 - Delete an entry by its index
It is sometimes necessary to delete an entry from the list. For this you can use the del function.Example
>>> list = ["a", "b", "c"]
>>> del list [1]
>>> list
['a', 'c']
4-2 - Delete an entry by its value
It is possible to delete an entry from a list with its value with the remove method.Example
>>> list = ["a", "b", "c"]
>>> list.remove ("a")
>>> list
['b', 'c']
5 - Invert the values of a list
You can invert the items in a list with the reverse method.Example
>>> list = ["a", "b", "c"]
>>> list.reverse ()
>>> list
['c', 'b', 'a']
6- Count the number of items & number of occurrences
6 -1 - Count the number of items in a list
It is possible to count the number of items in a list with the len function.Example
>>> list = [1,2,3,5,10]
>>> len (list)
5
6 -2 - Count the number of occurrences of a value
To know the number of occurrences of a value in a list, you can use the count method.Example
>>> list = ["a", "a", "a", "b", "c", "c"]
>>> list.count ("a")
3
>>> list.count ("c")
2
7 - Find the index of a value
The index method allows you to know the position of the item sought.Example
>>> list = ["a", "a", "a", "b", "c", "c"]
>>> list.index ("b")
3
8 - Manipulate a python list
Here are some tips for manipulating lists:Example
>>> list = [1, 10, 100, 200, 300]
>>> list [0]
1
>>> list [-1] # Look for the last occurrence
300
>>> list [-4:] # Displays the last 4 occurrences
[300, 200, 100, 10]
>>> list [:] # Displays all occurrences
[10, 100, 200, 300]
>>> list [2: 4] = [5, 7]
[1, 10, 5, 7, 300]
>>> list [:] = [] # empty the list
[]
9 - Loop on a python list
To display the values of a list, you can use a loop:Example
>>> list = ["a", "b", "c"]
>>> for letter in list:
... print letter
...
a
b
c
If you want also to recover the index, you can use the enumerate function:Example
>>> for letter in enumerate(list):
... print(letter)
...
(0, 'a')
(1, 'd')
(2, 'm')
# The values returned by the loop are tuples.
10 - Copy a list
Many beginners make the mistake of copying a list this way>>> x = [1,2,3]
>>> y = x
But if you change a value in the list y, the list x will also be affected by this change:>>> x = [1,2,3]
>>> y = x
>>> y [0] = 4
>>> x
[4, 2, 3]
In fact this syntax makes it possible to work on the same element named differently So how to copy a list that will be independent?Exemple: how to copy a list independently
>>> x = [1,2,3]
>>> y = x [:]
>>> # even if you change the values of y, those of x remain unchanged
>>> y[0] = 9
>>> x
[1, 2, 3]
>>> y
[9, 2, 3]
For more complex data, you can use the deepcopy function of the copy module
Example
>>> import copy
>>> x = [[1,2], 2]
>>> y = copy.deepcopy (x)
>>> y [1] = [1,2,3]
>>> x
[[1, 2], 2]
>>> y
[[1, 2], [1, 2, 3]]
11 - Transform a list into a string and vice versa
11 - 1 - Transform a string into a list
Sometimes it can be useful to turn a string into a list. This is possible with the split method.Example
>>> my_chaine = "Mac: Lorenz: USA"
>>> my_chaine.split (":")
['Mac', 'Lorenz', 'USA']
11 - 2 - Transform a list into a string
The opposite is possible with the "join" method.Example
>>> list = ['Mac', 'Lorenz', 'USA']
>>> ":". join (list)
'Mac: Lorenz: USA '
12 - Find an item in a list
To find out if an item is in a list, you can use the keyword in this way:Example
>>> list = [1,2,3,5,6]
>>> 5 in list
true
>>> 17 in list
false
13 - The range function
The range function generates a list consisting of a simple arithmetic sequence.Example
>>> range (10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
14 - Extend a list by a list
To put two lists end to end, you can use the extend method
Example
>>> x = [1, 2, 3, 4]
>>> y = [4, 5, 1, 0]
>>> x.extend (y)
>>> print(x)
[1, 2, 3, 4, 4, 5, 1, 0]
15 - Tips and tricks
Examples of various tricks
#Display the first 2 items of a list
>>> list = [1,2,3,4,5]
>>> list [: 2]
[1, 2]
#Display the last item of a list:
>>> list = [1, 2, 3, 4, 5, 6]
>>> list [-1]
6
#Display the 3rd element from the end:
>>> list = [1, 2, 3, 4, 5, 6]
>>> list [-3]
4
#Show the last 3 elements of a list:
>>> list = [1, 2, 3, 4, 5, 6]
>>> list [-3:]
[4, 5, 6]
#You can add two lists to combine them together using the +:
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> x + y
[1, 2, 3, 4, 5, 6]
#You can even mutiplier a list:
>>> x = [1, 2]
>>> x * 5
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
#What can be useful to initialize a list:
>>> [1] * 4
[1, 1, 1, 1, 1]
Younes Derfoufi
No comments:
Post a Comment