Skip to content

Python remove first element from list | Example code

  • by

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 perform the popleft to remove 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.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading