Python: Remove First Element from a List

A list is used in Python to store the sequence of different types of data. Python lists are mutable, which means that their elements can be changed after they have been created. Python, on the other hand, has six data types that can be used to store sequences, with the first two on the list being the most common and accurate.

A list is a collection of various types of values or objects. The list items are separated by a comma (,), which is enclosed in square brackets [].

Examples:

Input:

givenlist = ["Hello", "Btech" , "Geeks" ,"is" ,"new" ,"online" ]

Output:

['Btech', 'Geeks', 'is', 'new', 'online']

Explanation:

Hello is the first element in givenlist hence it is deleted.

Remove the First Element of a List

There are several ways to remove the first item from the list some of them are:

>Method #1:Using slicing

Lists can be cut in Python, as we all know. Slicing is a method of removing the first item from a list. The goal is to make a sublist that contains all of the elements of the list except the first one. Because the slice operation returns a new list, we must assign the new list to the original list. To accomplish this, use the expression l = l[1:], where l is your list. l[1:] is an abbreviation for l[0:len(l)-1].

Below is the implementation:

# Function which removes first element
def removeFirstElement(givenlist):
    # using slicing
    givenlist = givenlist[1:]
    # return the result list
    return givenlist


# Driver code
# given list
givenlist = ["Hello", "Btech", "Geeks", "is", "new", "online"]
# passing list to remove first element
print(removeFirstElement(givenlist))

Output:

['Btech', 'Geeks', 'is', 'new', 'online']

>Method #2:Using del keyword

Another way to delete an element from a list is to use its index in the del statement. It differs from the pop() function in that it does not return the removed element. Unlike the slicing feature, this does not generate a new list.

The index of first item in list is 0.

Below is the implementation:

# Function which removes first element
def removeFirstElement(givenlist):
    # using del keyword
    del givenlist[0]
    # return the result list
    return givenlist


# Driver code
# given list
givenlist = ["Hello", "Btech", "Geeks", "is", "new", "online"]
# passing list to remove first element
print(removeFirstElement(givenlist))

Output:

['Btech', 'Geeks', 'is', 'new', 'online']

>Method #3:Using pop() function

The list class in Python has a function called pop(index), which takes an optional argument index and deletes the element at that index. If no justification is given, it deletes the last element of the list by default.

The index of first element in list is 0.

Below is the implementation:

# Function which removes first element
def removeFirstElement(givenlist):
    # using pop function
    givenlist.pop(0)
    # return the result list
    return givenlist


# Driver code
# given list
givenlist = ["Hello", "Btech", "Geeks", "is", "new", "online"]
# passing list to remove first element
print(removeFirstElement(givenlist))

Output:

['Btech', 'Geeks', 'is', 'new', 'online']

>Method #4:Using remove() function

The list class in Python has a function remove(value) that deletes the first occurrence of a given value from the list. This can be used to remove the first item from the list. Select the first element from the list and pass it to the remove() function to accomplish this.

Below is the implementation:

# Function which removes first element
def removeFirstElement(givenlist):
    # getting first element of the given list
    firstelement = givenlist[0]
    # using remove function
    givenlist.remove(firstelement)
    # return the result list
    return givenlist


# Driver code
# given list
givenlist = ["Hello", "Btech", "Geeks", "is", "new", "online"]
# passing list to remove first element
print(removeFirstElement(givenlist))

Output:

['Btech', 'Geeks', 'is', 'new', 'online']

>Method #5:Using deque() function

A double-ended queue, or deque, allows you to add and remove elements from either end. The Deque module is a collection library module. It has methods for adding and removing elements that can be directly invoked with arguments.

Convert the given list to a deque and then pop an element from the left. Then add the remaining deque elements to the list.

Below is the implementation:

from collections import deque
# Function which removes first element


def removeFirstElement(givenlist):
    # converting given list to deque
    dequelist = deque(givenlist)
    # using popleft function to remove first element
    dequelist.popleft()
    # Converting deque to list
    givenlist = list(dequelist)
    # return the result list
    return givenlist


# Driver code
# given list
givenlist = ["Hello", "Btech", "Geeks", "is", "new", "online"]
# passing list to remove first element
print(removeFirstElement(givenlist))

Output:

['Btech', 'Geeks', 'is', 'new', 'online']

Related Programs: