• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Remove the first element from a list in Python

Remove the first element from a list in Python

Author: Aditya Raj
Last Updated: August 10, 2021

Lists are one of the most commonly used data structures in python. In python, we have different methods to perform operations on lists. In this article, we will look at different ways to remove the first element from a list using different methods like slicing, pop() method, remove() method and del operation.

Remove first element from list using slicing

In python, slicing is an operation to create a subpart of a string or a list. With slicing, we can access different parts of any string, tuple or a list. To perform slicing on a list named myList, we use the syntax myList[start, end, difference] where “start” and “end” are the indices at which the sliced list starts and ends in the original list respectively. The “difference” is used to select elements in a sequence. The elements are selected from the list at the index= “start+ n*difference” where n is a whole number and index should be less than “end”. 

To use slicing for removing the first element of the list, we will take out the sub list starting from the second element and we will leave behind the first element. Thus the first element will be removed from the list. This can be done as follows.

myList = [1, 2, 3, 4, 5, 6, 7]
print("Original List is:", myList)
myList = myList[1::1]
print("List after modification is:", myList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
List after modification is: [2, 3, 4, 5, 6, 7]

Remove first element from a list using pop() method

The pop() method is used to remove any element from the list from a specified index. It takes the index of the element as an optional input parameter and returns the element at the specified index after deleting it from the list. If no input parameter is passed, it will return the last element of the list after deleting it.

To delete the first element of the list using the pop() method, we will invoke the pop() method on the list and will pass an index of the first element, i.e. 0 as input. It will return the first element of the list after deleting it from the list as follows.

myList = [1, 2, 3, 4, 5, 6, 7]
print("Original List is:", myList)
myList.pop(0)
print("List after modification is:", myList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
List after modification is: [2, 3, 4, 5, 6, 7]

Using remove() method

he remove() method is used to remove the first occurrence of any element in the list. The remove() method, when invoked, takes the element to be deleted from the list as input and deletes the element from the list. When the element is not present in the list, it raises ValueError. To handle the ValueError, you can use exception handling using python try except blocks.

To delete the first element of the list using the remove() method, we will access the first element of the list using its index i.e. 0. Then we will pass the element as a parameter to the remove() method. This will delete the first element of the list as follows.

myList = [1, 2, 3, 4, 5, 6, 7]
print("Original List is:", myList)
element = myList[0]
myList.remove(element)
print("List after modification is:", myList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
List after modification is: [2, 3, 4, 5, 6, 7]

Remove first element from a list using del keyword

The del keyword in python is used to delete objects. In python, every variable points to an object. As we know that the first element of the list points to an object in the memory, we can also delete it using del keyword as follows.

myList = [1, 2, 3, 4, 5, 6, 7]
print("Original List is:", myList)
del myList[0]
print("List after modification is:", myList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
List after modification is: [2, 3, 4, 5, 6, 7]

Conclusion

In this article, we have seen different ways to delete the first element from a list in python. We have used slicing, pop() method, remove() method and del keyword to delete the first element from the list. To learn more about lists, you can read this article on list comprehension. Stay tuned for more informative articles.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us