-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #619 from realpython/how-to-remove-item-from-list-…
…python Sample code for the article on removing items from list
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# How to Remove Items From Lists in Python | ||
|
||
This folder provides the code examples for the Real Python tutorial [How to Remove Items From Lists in Python](https://realpython.com/how-to-remove-item-from-list-python/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"] | ||
print(books.pop(0)) | ||
print(books) | ||
|
||
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"] | ||
read_books = [] | ||
read = books.pop(0) | ||
read_books.append(read) | ||
print(read_books) | ||
print(books) | ||
|
||
books = ["Dragonsbane", "The Hobbit", "Wonder", "Wonder", "Jaws", "Jaws"] | ||
del books[2] | ||
print(books) | ||
del books[-1] | ||
print(books) | ||
|
||
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"] | ||
books.remove("The Hobbit") | ||
print(books) | ||
|
||
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"] | ||
books.remove("The Two Towers") | ||
print(books) | ||
|
||
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"] | ||
del books[0:3] | ||
print(books) | ||
|
||
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws", "It"] | ||
del books[-3:-1] | ||
print(books) | ||
|
||
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws", "It"] | ||
books.clear() | ||
print(books) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
phone_numbers = [ | ||
"54123", | ||
"54123", | ||
"54123", | ||
"54456", | ||
"54789", | ||
"54789", | ||
] | ||
for phone_number in phone_numbers[:]: | ||
if phone_numbers.count(phone_number) > 1: | ||
phone_numbers.remove(phone_number) | ||
print(phone_numbers) | ||
|
||
|
||
phone_numbers = [ | ||
"54123", | ||
"54123", | ||
"54123", | ||
"54456", | ||
"54789", | ||
"54789", | ||
] | ||
phone_numbers = list(dict.fromkeys(phone_numbers)) | ||
print(phone_numbers) | ||
|
||
phone_numbers = [ | ||
"54123", | ||
"54123", | ||
"54123", | ||
"54456", | ||
"54789", | ||
"54789", | ||
] | ||
set(phone_numbers) | ||
print(phone_numbers) |