Python learning road ------ list operation (complete)

list

Each element in the sequence is assigned a number - its position, or index, where the first index is 0, the second index is 1, and so on. Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members.

create a list

list_name = [elem1,elem2,…] comma separates each element, surrounded by square brackets

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
list_num =  [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] 
list_alpha =  [ 'a' , ' b' , 'c' , 'd' , 'e' , 'f' , 'g'] 
print ( list_a) 
print ( list_num ) 
print ( list_alpha ) 
'''
['Baidu', 'Google', 'Chorme', 2011, 1999]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
['a', 'b', 'c', 'd', 'e', ​​'f', 'g']
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

range() can create a list of values

mylist =  list ( range ( 10 ) ) 
print ( mylist )

'''
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

access the values ​​in the list

list[index] , note that index starts from 0

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
list_num =  [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] 
list_alpha =  [ 'a' , ' b' , 'c' , 'd' , 'e' , 'f' , 'g' ]

print ( 'list_a[2]:' , list_a [ 2 ] ) 
print ( 'list_num[4]:' , list_num [ 4 ] ) 
print ( 'list_alpha[-3]:' , list_alpha [ - 3 ] )

'''
list_a[2]: Chorme
list_num[4]: 5
list_alpha[-3]: e
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

update list

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
print ( 'old list_a[2]:' , list_a [ 2 ] ) 
list_a [ 2 ]  =  'None' 
print ( 'new list_a[2 ] ]:' , list_a [ 2 ] )

'''
old list_a[2]: Chorme
new list_a[2]: None
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

remove list element

del: delete according to the index position , or delete the entire data object

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
print ( 'old list_a[2]:' , list_a [ 2 ] ) 
del list_a [ 2 ] 
print ( 'new list_a[2]:' , list_a [ 2 ] )

'''
old list_a[2]: Chorme
new list_a[2]: 2011
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

remove : removes a single element, removes the first eligible element, removes by value

>>> str =  [ 1 , 2 , 3 , 4 , 5 , 2 , 6 ] >>> str .remove ( 2 ) >>> str [ 1 , 3 , 4 , 5 , 2 , 6 ] _ _ _ _
 
 
     
  • 1
  • 2
  • 3
  • 4

pop : delete single or multiple elements, bitwise delete, delete according to index

>>> str =  [ 1 , 2 , 3 , 4 , 5 , 2 , 6 ] >>> str .pop ( 5 ) 2 >>> str [ 1 , 2 , 3 , 4 , 5 , 6 ] _ _ _ _
 

 
     
  • 1
  • 2
  • 3
  • 4
  • 5

List interception and splicing

List interception:

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
print ( 'list_a[1]-list_a[3]:' , list_a [ 1 : 4 ] )

>>> list_a [ 1 ] - list_a [ 3 ] : [ ' Google' , 'Chorme ' , 2011 ]   
  • 1
  • 2
  • 3
  • 4

List concatenation:

list_num =  [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] 
print ( 'old list_num:' , list_num ) 
list_num +=  [ x ** 2  for x in list_num ] 
print ( 'new list_num:' , list_num )

>>> old list_num : [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] >>> new list_num : [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , 81 , 64 , 49 , 36 , 25          
               ,  16 ,  9 ,  4 ,  1 ,  0 ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

List copy:

List copy has deep copy and shallow copy

Deep copy: Modifying the copied list will not affect the copied list

  1. N=M[:]
  2. N = M.copy()

Example 1:

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
list_copy = list_a [ : ] 
print ( list_copy ) 
list_copy [ 0 ]  =  "Hello"    # will not modify list_a 
print ( list_copy ) 
print ( list_a )

'''
['Baidu', 'Google', 'Chorme', 2011, 1999]
['Hello', 'Google', 'Chorme', 2011, 1999]
['Baidu', 'Google', 'Chorme', 2011, 1999]

'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Example 2:

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
list_copy = list_a . copy ( ) 
print ( list_copy ) 
list_copy [ 0 ]  =  "Hello" 
print ( list_copy ) 
print ( list_a )

'''
['Baidu', 'Google', 'Chorme', 2011, 1999]
['Hello', 'Google', 'Chorme', 2011, 1999]
['Baidu', 'Google', 'Chorme', 2011, 1999]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Shallow copy: Modifying the copied list will affect the copied list

  1. M=[N,L,P]
  2. M = N

Example 1:

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
list_b =  [ 'China' , 'America' , 'French' , "Germany" ] 
list_copy =  [ list_a , list_b ] 
print ( list_copy ) 
print ( list_copy [ 0 ] [ 0 ] , list_copy [ 1 ] [ 3 ], end =  " " ) 
list_copy [ 0 ] [ 0 ]  =  "Hello" 
print ( "\n" ) 
print ( list_copy ) 
print ( list_a )   # Note the output, list_a is also changed

'''
[['Baidu', 'Google', 'Chorme', 2011, 1999], ['China', 'America', 'French', 'Germany']]
Baidu Germany

[['Hello', 'Google', 'Chorme', 2011, 1999], ['China', 'America', 'French', 'Germany']]
['Hello', 'Google', 'Chorme', 2011, 1999]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Example 2:

list_a =  [ 'Baidu' , 'Google' , 'Chorme' , 2011 , 1999 ] 
list_copy = list_a
 print ( list_copy ) 
list_copy [ 0 ]  =  "Hello"    #list_a is also modified 
print ( list_copy ) 
print ( list_a )   

'''
['Baidu', 'Google', 'Chorme', 2011, 1999]
['Hello', 'Google', 'Chorme', 2011, 1999]
['Hello', 'Google', 'Chorme', 2011, 1999]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

List Functions & Methods

The list contains the following functions:

len(list) returns the number of elements in the list:

list_num =  [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] 
print ( len ( list_num ) )

>> > 10
  • 1
  • 2
  • 3
  • 4

max(list) returns the maximum element value of the list element:

list_num =  [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] 
print ( max ( list_num ) )

>> > 9
  • 1
  • 2
  • 3
  • 4

min(list) returns the minimum element value of the list element:

list_num =  [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] 
print ( min ( list_num ) )

>> > 0
  • 1
  • 2
  • 3
  • 4

list(seq) converts tuples to lists:

Tuple1 =  ( 2 , 5 , 27 , 28 ) 
print ( Tuple1 ) 
list  =  list ( Tuple1 ) 
print ( list )

>>> ( 2 , 5 , 27 , 28 ) >>> [ 2 , 5 , 27 , 28 ] _ _   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
The list contains the following methods:

list.append(obj) adds a new object to the end of the list

list_num =  [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] 
print ( 'old list_num:' , list_num ) 
list_num . append ( '2019' ) 
print ( 'new list_num:' , list_num )

>>> old list_num : [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] >>> new list_num : [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , '2019' ]          
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

list.count(obj) counts the number of times an element appears in the list

list_num =  [ 9 , 8 , 6 , 6 , 6 , 4 , 3 , 2 , 6 , 0 ] 
print ( list_num . count ( 6 ) )

>> > 4
  • 1
  • 2
  • 3
  • 4

list.extend(seq) appends multiple values ​​from another sequence at the end of the list at once

list_num =  [ 9 , 8 , 6 , 6 , 6 , 4 , 3 , 2 , 6 , 0 ] 
list_alpha =  [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' ] 
print ( 'old list_num:' , list_num ) 
list_num . extend ( list_alpha ) 
print ('new list_num:' , list_num )

'''
old list_num: [9, 8, 6, 6, 6, 4, 3, 2, 6, 0]
new list_num: [9, 8, 6, 6, 6, 4, 3, 2, 6, 0, 'a', 'b', 'c', 'd', 'e', ​​'f', 'g ']
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

list.index(obj) finds the index position where a value matches for the first time in the list

list_num =  [ 9 , 8 , 6 , 6 , 6 , 4 , 3 , 2 , 6 , 0 ]

print ( list_num . index ( 4 ) ) 
print ( list_num . index ( 15 ) ) #no   value of 15 will explode

>> > 5 
Traceback ( most recent call last ) : 
  File "<pyshell#6>" , line 1 ,  in  < module > 
    print ( list_num . index ( 15 ) ) 
ValueError :  15  is  not  in  list
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

list.insert(index,obj) inserts the object into the list

list_num =  [ 9 , 8 , 6 , 6 , 6 , 4 , 3 , 2 , 6 , 0 ] 
list_num . insert ( 6 , 999 ) 
print ( list_num ) 
list_num . insert ( 80 , 11 )   #index beyond the index range will place 
print ( list_num ) at the end of the list

'''
[9, 8, 6, 6, 6, 4, 999, 3, 2, 6, 0]
[9, 8, 6, 6, 6, 4, 999, 3, 2, 6, 0, 11]
'''

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

list.pop (index = -1) removes an element in the list and returns the value of the element, the last element by default

list_num =  [ 9 , 8 , 6 , 6 , 6 , 4 , 3 , 2 , 6 , 0 ] 
list_num .pop ( ) print ( list_num ) 
list_num .pop ( 6 ) print ( list_num ) _ _



'''
[9, 8, 6, 6, 6, 4, 3, 2, 6]
[9, 8, 6, 6, 6, 4, 2, 6]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
methoddescribe
list.remove(obj)Remove the first occurrence of a value in a list
list.reverse()reverse list elements
list.sort(key=None,reverse=False)Sort the original list, reverse = True descending, reverse = False ascending (default)
list.clear()clear the list
list.copy()copy list, deep copy
list_num =  [ 9 , 8 , 6 , 6 , 6 , 4 , 3 , 2 , 6 , 0 ]

list_num . remove ( 8 ) 
print ( list_num ) 
list_num . reverse ( ) 
print ( list_num ) 
list_num . sort ( ) 
print ( list_num ) 
list_num . sort ( reverse =  True ) 
print ( list_num ) 
list_num . clear ( ) 
print ( list_num )

'''
[9, 6, 6, 6, 4, 3, 2, 6, 0]
[0, 6, 2, 3, 4, 6, 6, 6, 9]
[0, 2, 3, 4, 6, 6, 6, 6, 9]
[9, 6, 6, 6, 6, 4, 3, 2, 0]
[]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Output a list by specifying the ordering of the elements in the list:

list_num =  [ ( 9 , 8 , 6 ) , ( 6 , 6 , 4 ) , ( 3 , 2 , 6 ) , ( 0 , 41 , 2 ) ]

def  takeSecond ( elem ) : 
	return elem [ 1 ]
	
def  takeThird ( elem ) : 
	return elem [ 2 ]
	
list_num.sort ( ) print ( list_num ) 
list_num.sort ( key = takeSecond ) print ( list_num ) 
list_num.sort ( key = takeThird ) print ( list_num ) _ _ _ _ _ _




'''
[(0, 41, 2), (3, 2, 6), (6, 6, 4), (9, 8, 6)]
[(3, 2, 6), (6, 6, 4), (9, 8, 6), (0, 41, 2)]
[(0, 41, 2), (6, 6, 4), (3, 2, 6), (9, 8, 6)]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Conversion between tuple tuple, dictionary dict, list list

tuple conversion: cannot convert to dictionary

>>> tup = ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) >>> tup . __str__ ( ) ' ( 1, 2, 3, 4, 5, 6, 7, 8) ' > > > list ( tup ) [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]    


 
       
  • 1
  • 2
  • 3
  • 4
  • 5

Dictionary conversion:

>> >  dict  =  { 'name' :  'Zara' ,  'age' :  7 } 
>> >  str ( dict ) 
"{'name': 'Zara', 'age': 7}" 
>> >  tuple ( dict ) 
( 'name' ,  'age' ) 
>>> tuple ( dict . values ( ) ) ( 'Zara ' , 7 ) >> > list ( dict ) [ 'name'
 
 
,  'age' ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

List conversion:

>>> nums = [ 1 , 3 , 5 , 7 , 9 , 11 , 13 ] ; >>> str ( nums ) ' [1, 3, 5, 7, 9, 11, 13 ] ' >>> tuple ( nums ) ( 1 , 3 , 5 , 7 , 9 , 11 , 13 )      
 

 
      
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: Python learning road ------ list operation (complete)

Topics in Python python list

Related: Python learning road ------ list operation (complete)