How to create a list:
a=[1,2,3,4,5]
Use a bracket like this to enclose an element in a comma-delimited list of elements, which is represented as a listing.
Common actions in the list:
1. Element Assignment:
a[0]=10
Added a 10 to the list index of 0, overwriting the original 1.
Results: a=[10,2,3,4,5]
2. Add elements:
A.append (6)
Here we use a method append (), the Append () method to add a new object at the end of the list.
Results: a=[10,2,3,4,5,6]
3. Delete the element:
Del A[0]
Here, we used the Del to delete the element with List index 0.
Results: a=[2,3,4,5,6]
Common methods in the list:
1.append:
A.append (7)# Add a new object at the end of the list
2.count:
A.count (2)# used to count the number of occurrences of 2 in List a
3.pop:
A.pop ()# removes the last element in the list by default and returns the value of the element A.pop (3)# removes the element at the specified position and returns the value of the element
4.insert:
A.insert (2,10) # Inserts a 10 at the index value of 2, the element on index 2 and the subsequent element index plus 1
5.remove:
A.remove# Remove the first match starting at index 0
6.sort:
A.sort ()# default ascending order
Use the reverse parameter:
A.sort (reverse=true)# reverse order
Strings can use Key=len:
A.sore (Key=len)# Sort by string by short to long A.sore (key=len,reverse=true)# Sort by string from long to short
(self-hing ai) pythony list