How to Remove Items From Lists in Python
by:
blow post content copied from Real Python
click here to view original post
Removing items from a Python list is a common task that you can accomplish with various techniques. Whether you need to remove an item by its position or value, Python has you covered. In this tutorial, you’ll explore different approaches to removing items from a list, including using .pop()
, the del
statement, and .remove()
.
The .remove()
method allows you to delete the first occurrence of a specified value, while .pop()
can remove an item by its index and return it. The del
statement offers another way to remove items by index, and you can also use it to delete slices of a list. The approach you choose will depend on your specific needs.
By the end of this tutorial, you’ll understand that:
- To remove an item from a list in Python, you can use various approaches like
.pop()
,del
,.remove()
, and.clear()
. - To remove items from a certain position in a list, you use the
.pop()
method. - To delete items and slices from a list in Python, you use the
del
statement. - You use the
.remove()
method to delete the first occurrence of a specified value from a list. - To remove all the items from a list, you use
.clear()
. - You can also remove duplicate items using a loop, dictionary, or set.
To get the most out of this tutorial, you should be familiar with basic Python list
topics like creating lists, adding items to a list, and accessing items in a list.
Get Your Code: Click here to download the free sample code that you’ll use to remove items from lists in Python.
Take the Quiz: Test your knowledge with our interactive “How to Remove Items From Lists in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
How to Remove Items From Lists in PythonIn this quiz, you'll test your understanding of removing items from lists in Python. This is a fundamental skill in Python programming, and mastering it will enable you to manipulate lists effectively.
How to Remove Specific Items From a List
One common operation you’ll perform on a Python list
is to remove specific list items. You may need to remove items based on their position in the list, or their value.
To illustrate how you can accomplish this task, suppose you’re creating a website for a public library. Your web app will allow users to save a list of books they would like to read. It should also allow them to edit and remove books from the list, as well as sort the list.
You can use a Python list to store the user’s reading list as a collection of book titles. For example, the reading list might look something like this:
>>> books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"]
Now that you have a list of books, you have several ways to remove a single, specific book from the list. One approach is to use the .pop()
method.
Removing Items Using the .pop()
Method
Sometimes, you may need to remove items at a certain position in a list. For example, in a public library app, users might select books to remove by ticking checkboxes in the user interface. Your app will delete each selected item based on its index, which is the item’s position in the list.
If you know the index of the item you want to remove, then you can use the .pop()
method. This method takes the item’s index as an optional argument and then removes and returns the item at that index. If you don’t pass an index argument to the method call, then .pop()
will remove and return the last item in the list.
Note that Python lists use zero-based indexing for positioning, which means that the first element in a list is at index 0, the second element is at index 1, and so on. With that in mind, here’s an example of how you can use .pop()
to remove and display the first element in your books
list:
>>> books.pop(0)
'Dragonsbane'
You invoke the .pop()
method on the books
list with an index of 0
, indicating the first element in the list. This call removes the first title, Dragonsbane, from the list and then returns it.
If you check the content of your list after running this code, then you’ll notice that Dragonsbane isn’t there anymore:
>>> books
['The Hobbit', 'Wonder', 'Jaws']
Here, you display the book list again after the .pop()
call. You can see that your list is now one element shorter because .pop()
removed the first title.
As you learned earlier in the tutorial, .pop()
removes an item and also returns its value, which you can then use for other operations. For example, suppose the library app also allows users to store a separate list of books they’ve read. Once the user has read a book, they can remove it from the initial book list and transfer the title to the read list:
>>> books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"]
>>> read_books = []
>>> read = books.pop(0)
>>> read_books.append(read)
>>> read_books
['Dragonsbane']
>>> books
['The Hobbit', 'Wonder', 'Jaws']
On the second line in the example, you create a new, empty list called read_books
to store the names of the books the user has read. Next, you use the .pop()
method to remove the first title from the original book list and store it in a variable. Then, you use .append()
to add the stored title to the read_books
list.
Read the full article at https://realpython.com/remove-item-from-list-python/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
December 23, 2024 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment