Python list remove first item

Python: Remove first element from a list (5 Ways)

This article will discuss different ways to delete the first element from a list in Python.

Table of Contents

Remove the first element from a list in Python using the pop() function

In Python, the list class provides a function pop(index); it accepts an optional argument index and deletes the element at the given index. Let’s use this function to remove the last element from a list,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59] # Remove first element from list in python list_of_num.pop(0) print(list_of_num)

As we provided the 0 as the index argument in the pop() function, it deleted the first item of the list.

Frequently Asked:

Remove the first element from a list in Python using slicing

We can slice the list to remove the first element. To slice a list, provide the start and end index in the subscript operator. For example,

Читайте также:  Оператор display в питоне

It will select the elements from index positions start to end-1. If the start index is not provided, it selects from the first element of list and if end index is not provided, it selects until the end of the list. If the list has N elements, then slice the list to select elements from index position 1 to N. So, to delete the first element from a list, select the elements from 1 to end. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59] # Remove first element from list in python list_of_num = list_of_num[1:] print(list_of_num)

It deleted the first element from the list.

Remove the first element from a list in Python using del keyword

To delete the last element from a list, select the first element from the list and give it to the del keyword to delete it. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59] # Remove first element from list in python del list_of_num[0] print(list_of_num)

It deleted the first item from the list.

Remove the first element from a list in Python using the remove() function

In Python, the list class provides a function remove(value) to delete the first occurrence of a given value from the list. We can use this to delete the first element of the list. For this, select the first element from the list and pass it to the remove() function,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59] # Remove first element from list in python list_of_num.remove(list_of_num[0]) print(list_of_num)

It deleted the first element from the list.

Remove the first element from the list in Python using deque

Convert the given list to deque and pop an element from left. Then cast the remaining elements in deque to the list. For example,

from collections import deque list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59] # Remove first element from list in python queue = deque(list_of_num) queue.popleft() list_of_num = list(queue) print(list_of_num)

It deleted the first element from the list.

Summary

We learned about different ways to delete the first element from a list in Python.

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Python list remove first element: – Python: Remove First Element from a List

BTech Geeks

Python list remove first element: 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 [].

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

Explanation:

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

Remove the First Element of a List

Remove the first element of a list python: There are several ways to remove the first item from the list some of them are:

>Method #1:Using slicing

Python remove first item from list: 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))
['Btech', 'Geeks', 'is', 'new', 'online']

>Method #2:Using del keyword

Python list remove first: 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))
['Btech', 'Geeks', 'is', 'new', 'online']

>Method #3:Using pop() function

Python pop first element from list: 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))
['Btech', 'Geeks', 'is', 'new', 'online']

>Method #4:Using remove() function

Remove first item from list python: 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))
['Btech', 'Geeks', 'is', 'new', 'online']

>Method #5:Using deque() function

Removing first element from list python: 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))
['Btech', 'Geeks', 'is', 'new', 'online']

Related Programs:

Источник

Оцените статью