1. create new_list1 [TV, Car, Cloth, Food] new_list2list ([TV, Car, Cloth, Food]) print (new_list1) print (new_list2) in list: [TV, car, Cloth, Food] [TV, Car... 1. create a list
New_list1 = ['TV', 'car', 'body', 'food']
New_list2 = list (['TV', 'car', 'body', 'food'])
Print (new_list1)
Print (new_list2)
Running result:
['TV', 'car', 'cloth ', 'food']
['TV', 'car', 'cloth ', 'food']
2. list common methods:
1. append
New_list1 = ['TV', 'car', 'body', 'food']
Print (new_list1)
New_list1.append ('water') # append 'water'
Print (new_list1)
Running result:
['TV', 'car', 'cloth ', 'food']
['TV', 'car', 'cloth ', 'food', 'water']
2. count
New_list1 = ['TV', 'car', 'body', 'food', 'food']
Print (new_list1.count ('food') # count the number of times 'food' is in the list => 2
3. extend
New_list1 = ['TV', 'car', 'body', 'food', 'food']
New_list1.extend ([11,22, 33, 'cart'])
Print (new_list1)
Running result:
['TV', 'car', 'cloth ', 'food', 'food', 11, 22, 33, 'car']
4. sort
New_list1 = ['TV', 'car', 'body', 'food', 'food']
Print (new_list1)
New_list1.sort () # forward sorting
Print (new_list1)
New_list1.sort (reverse = True) # reverse sorting
Print (new_list1)
Running result:
['TV', 'car', 'cloth ', 'food', 'food']
['Car', 'br', 'food', 'food', 'TV'] # positive sorting result
['TV', 'food', 'food', 'body', 'car'] # reverse sorting result
5. len
New_list1 = ['TV', 'car', 'food', 'body', 'food']
Print (len (new_list1) => 5 # Number of print elements
6. remove
New_list1 = ['TV', 'car', 'food', 'body', 'food']
New_list1.remove ('food ')
Print (new_list1)
Running result:
['TV', 'car', 'cloth ', 'food'] # remove deletes the first identical element found.
7. pop
New_list1 = ['TV', 'car', 'food', 'body', 'food']
New_list1.pop () # Delete an element randomly
Print (new_list1)
New_list1.pop (2) # specify to delete an element
Print (new_list1)
The running result is:
['TV', 'car', 'food', 'bry']
['TV', 'car', 'body'] #. The first element 'food' is deleted here'
8. index
New_list1 = ['TV', 'car', 'food', 'body', 'food']
Print (new_list1.index ('food') # return the index value of the specified element. if the same element is used, the first one is returned.
Running Result: 2
9. insert
New_list1 = ['TV', 'car', 'food', 'body', 'food']
New_list1.insert (2, 'hahahaha') # insert an element to a specified position
Print (new_list1)
The running result is:
['TV', 'car', 'hahaha', 'food', 'body', 'food']