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.
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:
Drop the first item from a list by the pop method in Python
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]
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
You can also use slicing to remove the first item from a list, here is the example
Drop the first item from a list by slicing in Python
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.