Python remove first element from list | Example code

Use the del keyword or pop() method to remove the first element from the list in Python. But there are more methods to do it:-

Methods remove the first element from a list:-

  • del keyword
  • list.pop() function
  • Using Slicing
  • collections.deque

Examples remove the first element from the list in Python

Simple example code.

Use the del keyword to remove the first element from a list

a_list = [1, 2, 3]

del a_list[0]

print(a_list)

Output:

Python remove first element from list

Use list.pop() to remove the first element from a list

This method will remove and return the first element.

a_list = [1, 2, 3]

res = a_list.pop(0)

print(res)
print(a_list)

Output:

1
[2, 3]

Using Slice notation

This method will not change the original list.

a_list = [1, 2, 3]

res = a_list[1:]

print(res)
print(a_list)

Output:

[2, 3]
[1, 2, 3]

Using deque() + popleft()

Convert the list into deque and then performing the popleft to removes the element of the list.

from collections import deque

a_list = [1, 2, 3]

res = deque(a_list)
res.popleft()

print(res)
print(a_list)

Output:

deque([2, 3])
[1, 2, 3]

Do comment if you have any doubts and suggestions on this Python list topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.