Remove first element from list in Python

In this post, we will see how to remove the first element from a list in python.


Using list.pop()

We can use list.pop() method to remove the first element from the list.

Output:

List Of Fruits are: [‘Orange’, ‘Apple’, ‘Grapes’, ‘Mango’] List Of Fruits after removing first element: [‘Apple’, ‘Grapes’, ‘Mango’] Removed Fruit: Orange

pop will raise index error if the list is empty.


Using del statement

We can use del statement to remove first
element.

Output:

List Of Fruits are: [‘Orange’, ‘Apple’, ‘Grapes’, ‘Mango’] List Of Fruits after removing first element: [‘Apple’, ‘Grapes’, ‘Mango’]

It raises IndexError if list is empty as it is trying to access an index that is out of range.


Using slicing

We can also use slicing to remove the first element. We can get a sublist of elements except the first element.

Output:

List Of Fruits are: [‘Orange’, ‘Apple’, ‘Grapes’, ‘Mango’] List Of Fruits after removing first element: [‘Apple’, ‘Grapes’, ‘Mango’]

Please note that slicing operation will return a new list, so this method is not recommended. We can obviously assign the list to the old one.

Using remove()

We can also use list’s remove() method to remove first element from the list. remove method takes value as argument and removes the first occurrence of value specified.

Output:

List Of Fruits are: [‘Orange’, ‘Apple’, ‘Grapes’, ‘Mango’] List Of Fruits after removing first element: [‘Apple’, ‘Grapes’, ‘Mango’]

It throws index error in case the list is empty.

That’s all about how to remove first element from list in python

Related Posts

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • Write array to CSV in Python
    30 August

    Python write array to CSV

    Table of ContentsUsing the pandas.to_csv() function to write array to CSV fileUsing the numpy.tofile() function to write array to CSV fileUsing the numpy.savetxt() function to write array to CSV fileUsing the file-handling methods to write array to CSV fileUsing the writerows() function to write array to CSV file A CSV file is a simple text […]

  • Multiply in Python
    30 August

    Multiply in Python

    Table of ContentsMultiply operator in PythonMultiplying a string with an integerMultiplying all the elements of a listUse the for loop to multiply all the elements of a listUse the functools.reduce() function to multiply all the elements of a listUse the numpy.prod() function to multiply all the elements of a listUse the math.prod() function to multiply […]

  • Auto clicker in Python
    30 August

    How to make auto clicker in Python

    Table of ContentsUsing the pynput module to create an auto clickerUsing the pyautogui module to create an auto clickerUsing the pywin32 (winauto32api) module to create an auto clicker Python has inbuilt modules and methods which allow it to detect and perform keyboard and mouse inputs. We can create an auto-mouse clicker in Python. An auto-clicker […]

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write variable to file in Python
    30 August

    Write variable to file in python

    Table of ContentsUsing the str() functionUsing the repr() functionUsing the string formattingUsing the pickle.dump() functionUsing the numpy.savetxt function File handling is an important concept of programming. Python allows us to read and write content to an external file. In this article, we will discuss several ways to write variable to file in Python. First, let […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.