How do I remove the first item from a list?
You can use the list method pop(index)
to remove the first item in a list. The pop()
method removes the item at the specified index (in this case, 0, as the first item has an index of 0 in Python) and returns the removed item. Here's an example:
my_list = [1, 2, 3, 4, 5]
first_item = my_list.pop(0)
print(first_item) # Output: 1
print(my_list) # Output: [2, 3, 4, 5]
Watch a video course
Python - The Practical Guide
You can also use slicing to remove the first item from a list, here is the example
my_list = [1, 2, 3, 4, 5]
my_list = my_list[1:]
print(my_list) # Output: [2, 3, 4, 5]
Note that the pop()
method modifies the original list, whereas slicing creates a new list.