In this article, we will take a look at 3 ways to remove first element with example programs and their explanation.
Method 1: Remove first element with pop()
Python list pop() method removes and returns the last element from the list.pop()
accepts an integer and removes the element at that index. So, to remove the first element, pop()
should be invoked with 0 as argument, which is the index of first element.pop()
will raise an IndexError
if the list is empty or the index is invalid.
Python docs for pop()
state,
Remove and return item at index (default last)
Example program follows
numbers=[1,2,3,4] print ('Original List:', numbers) deleted=numbers.pop(0) print('List with first element removed', numbers) print('Element removed is:', deleted)
Output is
Element removed is: 1
Note that pop()
modifies the original list.
Method 2: Remove first list element with del
Python del
statement is used to remove an element from the list by its index. del
followed by the element selected with its index will delete it from the list.
Example,
numbers=[1,2,3,4] print ('Original List:', numbers) del numbers[0] print('List with first element removed', numbers)
Here, numbers[0]
will return the first list element and del
before it will delete the element with this value from the list.
Output is
List with first element removed [2, 3, 4]
Remember that del
will delete only the element at given index. Don’t assume that del numbers[0] will translate to del 1
and it will delete all the elements with this value.
So, del numbers[1]
will delete the element at index 1 from the list.
del
does not return the element removed unlike pop()
.del
modifies the original list, unlike slicing.
Method 3: Remove first list element with slicing
Python list slicing allows you to create a sub-list from a list by selecting elements from the list in a range.
List slicing has below syntax
object_name[start:stop:step]
where,
- object_name: Variable that refers to a list.
- start: Index where slicing starts. It is optional and defaults to 0(first list element).
stop: Index where slicing ends. Element at stop index is not included in the result, that is, element till index (stop – 1) is included in the slice.
It is optional and defaults to the length of list. - step: Interval between the index values. It is optional and defaults to 1.
This will return a list with elements between indexes start and (stop – 1) of the original list. Remember that slicing returns a new list and the original list is not modified.
start and stop indexes can be negative, which means that indexing starts from right to left.
In order to remove the first list element, we need to extract a list from second to last element.
So, with slicing:
start must be 1, the second list element.
stop must be omitted, since it is the end of list, by default.
Thus, it will select all the elements from index 1 till the last element. Example,
numbers = [1,2,3,4] print ('Original List:', numbers) numbers = numbers[1:] print('List with first element removed', numbers)
Output will be
List with first element removed [2, 3, 4]
That is all on different ways to remove the first list element in python.
Hope the article was useful.