Python remove first items from list

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,

Читайте также:  Try java in browser

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.

Источник

Remove First Element from List in Python

Remove First Element from List in Python

Python lists are one of the most used sequential data structures when it comes to storing and manipulating data easily while programming. Lists are one collection that simplifies the life of programmers to a great extent. However, when it comes to removing items from lists, there are multiple ways in which this can be done. Removing elements is not just a matter of removing a value from a list, but also of making sure that the other elements in the list remain valid. In this article, we will take a look at five different ways that you can use to remove the first element from the lists. But before that, let us take a brief look at the list of data structures in python programming.

What are Lists in Python?

A list is a Python data structure that can be used to represent a collection of data elements to organize data while programming. They are ordered and mutable. This means that you can add new items to the list, remove items one-by-one, and rearrange items according to the order in which they appear. Listing elements with commas between them inside the square brackets([]) is a convenient way to organize data while using a list data structure. To learn more about lists in Python, refer to our article “3 Ways to Convert List to Tuple in Python”.

For Example

sample_list = ["favtutor", 1, 2.30] print(sample_list)

How to Remove First Element from a List in Python?

Below are the 5 common methods by which you can remove the first element of any given list in python:

1) Using pop()

The pop() method is one of the list object method used to remove an element from the list and returns it. While using the pop() method, you have to specify the index value of the element in the list as an argument and return the popped out element as the desired output. As we wish to remove the first element of the list here, we pass “0” as the parameter inside the method. If the particular element to be removed is not present in the list, then the error “IndexError” will be raised.

Check out the below example to understand the working of the pop() method and remove the desired element from the list.

For Example

sample_list = [1, 2, 3, 4, 5] a = sample_list.pop(0) print(sample_list)

2) Using remove() method

The remove () method is one of the most common methods used to remove and return the list element. Unlike the pop() method, when you make use of the remove() method, you have to specify the particular element to be removed as a method parameter. If the element is repeated inside the list, then the first occurrence of the element will be removed. At the same time, if the element is not found in the list, then the error “ValueError” will be returned.

Below example shows the working of the remove() method in detail:

For Example

sample_list = [1, 2, 3, 4, 5] sample_list.remove(sample_list[0]) print(sample_list)

3) Using del operator

Del operator has similar working as pop() method to remove the elements from the lists in python. The del operator removes the element from the list at a specified index location but does not return the removed item, unlike the pop() method. So the operator takes the element to be removed as the argument and deletes it from the given list. The main advantage of this element is that it supports removing more than one element at a time from the list. The program will return the “IndexError” as output when the index of the element is out of range.

For Example

sample_list = [1, 2, 3, 4, 5] del sample_list[0] print(sample_list)

4) Using slicing method

Slicing is one of the techniques used in python programming to manipulate strings and lists as data structures. We can use the slicing technique to slice the list from the second element and separate it from the first element. Later, you can return this element as the output as shown in the below example. Remember that this method does not inplace the removal of elements like the above methods.

For Example

sample_list = [1, 2, 3, 4, 5] sample_list = sample_list[1:] print(sample_list)

5) Using deque() method

This is the least used method to remove the elements from the given list in python programming. In this method, we convert the list into the deque and then use the popleft() method which helps to return the first element from the front of the list. Remember that to implement this method, you have to import the python deque at the beginning of the program using the “import” keyword. Check out the below example to understand the working of deque in detail.

For Example

from collections import deque sample_list = [1,2,3,4,5] queue = deque(sample_list) queue.popleft() sample_list = list(queue) print(sample_list)

Conclusion

As lists are the most used data structure while programming in python, there are times when you wish to add, update, or remove certain specified elements from the list. Updating these changes manually after completing the entire program is feasibly impossible. Therefore, we have mentioned the 5 most common and popular methods to remove the first element from the list in python. You can make use of any of these methods according to your preference, however, the remove() method is most recommended by the experts. For more such technical knowledge, check out our blogs on different programming languages.

Источник

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