.feed-links {display:none !important;} -->

Python lists

 The lists (or list / array ) in python is a variable in which we can put more variables.

Create a list in python

To create a list , nothing could be simpler:

>>>  list  =  []

You can see the contents of the list by calling it like this:

>>>  list 
< type  'list' >

Add a value to a python list

You can add any values ​​you want when creating the python list :

> > > List =  [ 1 , 2 , 3 ] 
> > > list
 [ 1 ,  2 ,  3 ]

Or add them after creating the list with the method append (which means "add" in English):

>>>  list  =  [] 
>>>  list 
[] 
>>>  list . append ( 1 ) 
>>>  list 
[ 1 ] 
>>>  list . append ( "ok" ) 
>>>  list 
[ 1 ,  'ok' ]

We see that it is possible to mix in the same list variables of different type. You can also put a list in a list.

Display an item from a list

To read a list, we can ask to see the index of the value that interests us:

>>>  list  =  [ "a" , "d" , "m" ] 
>>>  list [ 0 ] 
'a' 
>>>  list [ 2 ] 
'm'

The first item always starts with index 0. To read the first item we use the value 0, the second we use the value 1, and so on.

It is also possible to modify a value with its index

> > > List =  [ "a" , "d" , "m" ] 
> > > list [ 0 ] 
'a' 
> > > list [ 2 ] 
'm' 
> > > list [ 2 ]  =  "z" 
> > > list
 [ 'a' ,  'd' ,  'z' ]

Delete an entry with an index

Sometimes it is necessary to remove an entry from the list. For that you can use the function del .

>>>  list  =  [ "a" ,  "b" ,  "c" ] 
>>>  del  list [ 1 ] 
>>>  list 
[ 'a' ,  'c' ]

Delete an entry with its value

It is possible to remove an entry from a list with its value with the method remove .

>>>  list  =  [ "a" ,  "b" ,  "c" ] 
>>>  list . remove ( "a" ) 
>>>  list 
[ 'b' ,  'c' ]

Invert the values ​​of a list

You can invert the items of a list with the method reverse .

>>>  list  =  [ "a" ,  "b" ,  "c" ] 
>>>  list . reverse () 
>>>  list 
[ 'c' ,  'b' ,  'a' ]

Count the number of items in a list

It is possible to count the number of items in a list with the function len .

>>>  list  =  [ 1 , 2 , 3 , 5 , 10 ] 
>>>  len ( list ) 
5

Count the number of occurrences of a value

To know the number of occurrences of a value in a list, you can use the method count .

>>>  list  =  [ "a" , "a" , "a" , "b" , "c" , "c" ] 
>>>  list . count ( "a" ) 
3 
>>>  list . count ( "c" ) 
2

Find the index of a value

The method index allows you to know the position of the searched item.

>>>  list  =  [ "a" , "a" , "a" , "b" , "c" , "c" ] 
>>>  list . index ( "b" ) 
3

Handle a list

Here are some tips for handling lists:

>>>  list  =  [ 1 ,  10 ,  100 ,  250 ,  500 ] 
>>>  list [ 0 ] 
1 
>>>  list [ - 1 ]  # Find the last occurrence 
500 
>>>  list [ - 4 :]  # Display the Last 4 occurrences 
[ 500 ,  250 ,  100 ,  10 ] 
>>>  list [:]  # Display all occurrences 
[ 1 , 10 ,  100 ,  250 ,  500 ] 
>>>  list [ 2 : 4 ]  =  [ 69 ,  70 ] 
[ 1 ,  10 ,  69 ,  70 ,  500 ] 
>>>  list [:]  =  []  # empty list 
[]

Loop over a list

To display the values ​​of a list, we can use a loop:

>>>  list  =  [ "a" , "d" , "m" ] 
>>>  for  letter  in  list : 
...      print  letter 
...  
a 
d 
m

If you also want to retrieve the index, you can use the function enumerate .

>>>  for  letter  in  enumerate ( list ): 
...      print  letter 
...  
( 0 ,  'a' ) 
( 1 ,  'd' ) 
( 2 ,  'm' )

The values ​​returned by the loop are tuples.

Copy a list

Many beginners make the mistake of copying a list this way

>>>  x  =  [ 1 , 2 , 3 ] 
>>>  y  =  x

However, if you change a value of the list y , the list x will also be affected by this modification:

>>>  x  =  [ 1 , 2 , 3 ] 
>>>  y  =  x 
>>>  y [ 0 ]  =  4 
>>>  x 
[ 4 ,  2 ,  3 ]

In fact this syntax allows you to work on the same element named differently

So how do you copy a list that will be independent?

>>>  x  =  [ 1 , 2 , 3 ] 
>>>  y  =  x [:] 
>>>  y [ 0 ]  =  9 
>>>  x 
[ 1 ,  2 ,  3 ] 
>>>  y 
[ 9 ,  2 ,  3 ]

For more complex data, you can use the deepcopy module function copy

>>>  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 ] ]

Transform a string into a list

Sometimes it can be useful to turn a string into a list. This is possible with the method split .

>>>  ma_chaine  =  "Olivier: ENGEL: Strasbourg" 
>>>  ma_chaine . split ( ":" ) 
[ 'Olivier' ,  'ENGEL' ,  'Strasbourg' ]

Transform a list into a string

The reverse is possible with the " join " method .

>>>  list  =  [ "Olivier" , "ENGEL" , "Strasbourg" ] 
>>>  ":" . join ( list ) 
'Olivier: ENGEL: Strasbourg'

Find an item in a list

To find out if an item is in a list, you can use the in keyword this way:

>>>  list  =  [ 1 , 2 , 3 , 5 , 10 ] 
>>>  3  in  list 
True 
>>>  11  in  list 
False

The range function

The function range generates a list made up of a simple arithmetic sequence.

>>>  range ( 10 ) 
[ 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ]

Expand a list by a list

To put two lists end to end, you can use the method extend

>>>  x  =  [ 1 ,  2 ,  3 ,  4 ] 
>>>  y  =  [ 4 ,  5 ,  1 ,  0 ] 
>>>  x . extend ( y ) 
>>>  print  x 
[ 1 ,  2 ,  3 ,  4 ,  4 ,  5 ,  1 ,  0 ]

Permutations

The permutation of a set of elements is a list of all possible cases. If you need this functionality, no need to reinvent the wheel, itertools will take care of it for you.

>>>  from  itertools  import  permutations 
>>>  list ( permutations ([ 'a' ,  'b' ,  'c' ])) 
[( 'a' ,  'b' ,  'c' ),  ( 'a' ,  ' c ' ,  ' b ' ),  ( ' b ' ,  ' a ' ,  ' c ' ),  ( ' b ' ,  ' c ' ,  ' a ' ),  ( ' c ' ,  ' a ',  'b' ),  ( 'c' , 'b' ,  'a' )]

Swapping a List List

How to display all the possible cases of a list itself composed of a list? With the product tool from itertools :

>>>  from  itertools  import  product 
>>>  list ( product ([ 'a' ,  'b' ],  [ 'c' ,  'd' ])) 
[( 'a' ,  'c' ),  ( 'a' ,  'd' ),  ( 'b' ,  'c' ),  ( 'b' ,  'd' )]

Tips

Display the first 2 elements of a list

>>>  list  =  [ 1 , 2 , 3 , 4 , 5 ] 
>>>  list [: 2 ] 
[ 1 ,  2 ]

Display the last item in 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

Display 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 operator :

>>>  x  =  [ 1 ,  2 ,  3 ] 
>>>  y  =  [ 4 ,  5 ,  6 ] 
>>>  x  +  y 
[ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ]

You can even multiply a list:

>>>  x  =  [ 1 ,  2 ] 
>>>  x * 5 
[ 1 ,  2 ,  1 ,  2 ,  1 ,  2 ,  1 ,  2 ,  1 ,  2 ]

Which can be useful for initializing a list:

>>>  [ 0 ]  *  5 
[ 0 ,  0 ,  0 ,  0 ,  0 ]

No comments:

Post a Comment