Remove Last Element from a List

Python: Remove Last Element from a List

Lists are ordered sequences of objects that may contain a variety of different object types. Members of lists can also be duplicated. Lists in Python are similar to arrays in other programming languages.. However, there is one important distinction. Python lists can contain objects of different data types, while arrays can only contain elements of the same data type.

An index is a position in a list.

Given a list, the task is to remove last element from the given list.

Examples:

Input:

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

Output:

Btech Geeks is new online

Explanation:

platform is the last element in givenlist hence it is deleted.

Remove the final item from a list

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

Method #1: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.

Below is the implementation:

# Function which removes last element
def removeLastElement(givenlist):
    # using pop() function
    givenlist.pop()
    # return the result list
    return givenlist


# Driver code
# given list
givenlist = ["Btech", "Geeks", "is", "new", "online", "platform"]


# passing list to remove last element
print(removeLastElement(givenlist))

Output:

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

If the list is empty, the pop([i]) function throws an IndexError since it attempts to pop from an empty list.

Method #2:Using del keyword

The del statement is another way to delete an element from a list by using its index. It differs from the pop() function in that it does not return the element that has been removed. This does not generate a new list, unlike the slicing feature.

Below is the implementation:

# Function which removes last element
def removeLastElement(givenlist):
    # using del keyword
    del givenlist[-1]
    # return the result list
    return givenlist


# Driver code
# given list
givenlist = ["Btech", "Geeks", "is", "new", "online", "platform"]


# passing list to remove last element
print(removeLastElement(givenlist))

Output:

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

If the list is empty, the above code raises an IndexError because it tries to reach an index of the list that is out of control.

Method #3: Using slicing

In Python, we know that lists can be cut. Slicing is a technique for removing the last element from a list. The aim is to create a sublist that contains all of the list’s elements except the last one. We must assign the new list to the original list since the slice operation returns a new list. The expression l = l[:-1], where l is your list, can be used to accomplish this. The abbreviation l[:-1] stands for l[0:len(l)-1].

Below is the implementation:

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


# Driver code
# given list
givenlist = ["Btech", "Geeks", "is", "new", "online", "platform"]


# passing list to remove last element
print(removeLastElement(givenlist))

Output:

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

It should be noted that this function does not throw an error if the list is empty, but instead creates a copy of the list, which is not recommended.

Related Programs: