Social Icons

Thursday, March 22, 2018

Python Lists

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

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (1) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (169) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (33) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (140) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates