The list of list operation method summary in Python
- 2020-04-02 13:58:49
- OfStack
This article summarizes the common operations of lists in Python for your reference. Specific methods are as follows:
I. list created by Python:
sample_list = ['a',1,('a','b')]
Ii. Python list operation:
Suppose there is the following list:
sample_list = ['a','b',0,1,3]
1. Get a value in the list:
value_start = sample_list[0]
end_value = sample_list[-1]
2. Delete the first value of the list:
del sample_list[0]
3. Insert a value in the list:
sample_list[0:0] = ['sample value']
4. Get the length of the list:
list_length = len(sample_list)
5. List traversal:
for element in sample_list:
print(element)
Python list advanced operations/techniques
1. Generate a list of increasing values:
num_inc_list = range(30)
#will return a list [0,1,2,...,29]
2. Initialize the list with a fixed value:
initial_value = 0
list_length = 5
sample_list = [ initial_value for i in range(10)]
sample_list = [initial_value]*list_length
# sample_list ==[0,0,0,0,0]
You can also build on this to further consolidate and deepen your understanding of Python list operations.