list
The list is a built-in data type in Python. It is an ordered set, elements of which can be added or deleted at any time, and can contain elements of different data types. Can be included as an element in another list.
name = ['name1','name2','name3']
#####name = ('a','b','c',2,4,6) This is not a list, ---this is called tuple of tuples.
#name = 1,2,3 Not sure if
you can use the len() function to get the number of elements in the list
len(name)
The list is a built-in data type in Python. It is an ordered set, elements of which can be added or deleted at any time, and can contain elements of different data types. Can be included as an element in another list.
name = ['name1','name2','name3']
#####name = ('a','b','c',2,4,6) This is not a list, ---this is called tuple of tuples.
#name = 1,2,3 Not sure if
you can use the len() function to get the number of elements in the list
len(name)
Get elements
by index to access the element at each position in the list by index, the index starts from 0, if you want to use the last element, you can use -1.
name[0]
name[1]
name[-1]
when the index When it exceeds the range in the list, an IndexError will be reported. In order to prevent the index from being out of bounds, you can use len(name) -1 to mark the index range.
Append elements to the list
Append elements to the end
name.append('aaa') #Append elements 'aaa' at the end of the name list
# In this step, if an error is reported, it may be that [] was not used
to insert elements into the specified position when defining the list
name.insert(1,'jack') #Insert the element 'jack' at the index number 1 of the name list #I
tried it, if there are currently 5 elements in the list, use insert(10,'a') The method can insert the element 'a' at the end, but using name[9] will report an error, using name[5] The output is the element just inserted.
by index to access the element at each position in the list by index, the index starts from 0, if you want to use the last element, you can use -1.
name[0]
name[1]
name[-1]
when the index When it exceeds the range in the list, an IndexError will be reported. In order to prevent the index from being out of bounds, you can use len(name) -1 to mark the index range.
Append elements to the list
Append elements to the end
name.append('aaa') #Append elements 'aaa' at the end of the name list
# In this step, if an error is reported, it may be that [] was not used
to insert elements into the specified position when defining the list
name.insert(1,'jack') #Insert the element 'jack' at the index number 1 of the name list #I
tried it, if there are currently 5 elements in the list, use insert(10,'a') The method can insert the element 'a' at the end, but using name[9] will report an error, using name[5] The output is the element just inserted.
Delete an element from the list Delete
the element at the end
name.pop()
Delete the element at the specified index position
name.pop(2)
Replace an element, you can directly assign it to the corresponding element
name[3] = 'aaaaa'
the element at the end
name.pop()
Delete the element at the specified index position
name.pop(2)
Replace an element, you can directly assign it to the corresponding element
name[3] = 'aaaaa'
A list can be contained as an element by another list
name = ['a','b','c',['q','w','e'],'d','e',1,2, 3]
can be understood as:
sub = ['q','w','e']
name = ['a','b','c',sub,'d','e',1,2, 3]
name = ['a','b','c',['q','w','e'],'d','e',1,2, 3]
can be understood as:
sub = ['q','w','e']
name = ['a','b','c',sub,'d','e',1,2, 3]
If you want to display the element 'q', you can do it by name[3][1], which is the first element in the third element.
If there is no element in the list, then the length is 0.
If the list is included as an element by another list, len() is performed on the outermost list, and the list contained in the outermost element will be regarded as an element (obvious), For example:
a = ['a','b',['c','d','e','f'],'g']
The result of len(a) is 4.
If there is no element in the list, then the length is 0.
If the list is included as an element by another list, len() is performed on the outermost list, and the list contained in the outermost element will be regarded as an element (obvious), For example:
a = ['a','b',['c','d','e','f'],'g']
The result of len(a) is 4.