Python List: Remove the First Element - Two Methods
To remove the first element from a Python list, you can use the 'pop()' method with an index of 0:
my_list = [1, 2, 3, 4]
my_list.pop(0)
print(my_list) # Output: [2, 3, 4]
Alternatively, you can use slicing to create a new list without the first element:
my_list = [1, 2, 3, 4]
new_list = my_list[1:]
print(new_list) # Output: [2, 3, 4]
Note that the 'pop()' method modifies the original list, while slicing creates a new list.
原文地址: https://www.cveoy.top/t/topic/mYAx 著作权归作者所有。请勿转载和采集!