The list is a collection in Python, and the elements in the collection can be any data type, with the following usage:
Note: The following list of elements are in the case of a string, if the number or Boolean value is not added '
1> Building a list
Set name = [' Element 1 ', ' element 2 ', ' element 3 ', ...]
Example classmates = [' Hi ', ' hallo ', ' yellow ']
2> View a list
Enter the name of the collection directly
Example classmates
3> the length of a list, which has several elements
Len (collection name)
Example Len (classmates)
4> viewing elements in a list at a specific location
collection name [number]
Example Classmates[0] Note: The numbers here are the same as the C language starting with the 0 record
Python also supports reverse view
collection name [-number]
Example Classmates[-1]
5> adds new elements to the list at the end
Collection name. Append (' new element ')
Example Classmates.append (' Hola ')
6> inserting a new element at a specified position
Collection name. Insert (position, ' new element ')
Example Classmates.insert (1. ' Wow ')
7> Delete last Element
Collection name. Pop ()
Example Classmates.pop ()
8> delete the specified position element
Collection name. Pop (location)
Example Classmates.pop (1)
9> replacing the specified position element
collection name [position] = ' new element '
Example classmates[1] = ' haha '
10> contains another list in a list
Set name 1 = [' element 1 ', ' element 2 ', [' element 1 ', ' Element 2 '], ' element 3 ']
Example s = [' lala ', ' Heihei ', [' Xixi ', ' AA '], ' Lulu ']
11> to view an element of another list in a list
Collection name 1[location 1][position 2] Note: Position 1 must be the location of the included list, position 2 is also starting from 0 record
Example S[2][0]
List Usage Big Summary