From 21c0a2e05c13cc0773311c53d132236383e8aebd Mon Sep 17 00:00:00 2001 From: Geir Arne Hjelle Date: Wed, 18 Dec 2024 18:06:11 +0100 Subject: [PATCH] Final QA (#623) --- how-to-remove-item-from-list-python/README.md | 2 +- how-to-remove-item-from-list-python/books.py | 6 ++-- .../phone_book.py | 32 ++++--------------- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/how-to-remove-item-from-list-python/README.md b/how-to-remove-item-from-list-python/README.md index ac19483374..002ac70cac 100644 --- a/how-to-remove-item-from-list-python/README.md +++ b/how-to-remove-item-from-list-python/README.md @@ -1,3 +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/). \ No newline at end of file +This folder provides the code examples for the Real Python tutorial [How to Remove Items From Lists in Python](https://realpython.com/remove-item-from-list-python/). \ No newline at end of file diff --git a/how-to-remove-item-from-list-python/books.py b/how-to-remove-item-from-list-python/books.py index fd33651916..ad517c6d61 100644 --- a/how-to-remove-item-from-list-python/books.py +++ b/how-to-remove-item-from-list-python/books.py @@ -19,9 +19,9 @@ 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"] +# books.remove("The Two Towers") +# print(books) books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"] del books[0:3] diff --git a/how-to-remove-item-from-list-python/phone_book.py b/how-to-remove-item-from-list-python/phone_book.py index 5d38291b35..4444f84b91 100644 --- a/how-to-remove-item-from-list-python/phone_book.py +++ b/how-to-remove-item-from-list-python/phone_book.py @@ -1,35 +1,15 @@ -phone_numbers = [ - "54123", - "54123", - "54123", - "54456", - "54789", - "54789", -] +phone_numbers = ["54123", "54123", "54456", "54789", "54789", "54123"] for phone_number in phone_numbers[:]: - if phone_numbers.count(phone_number) > 1: + while phone_numbers.count(phone_number) > 1: phone_numbers.remove(phone_number) print(phone_numbers) -phone_numbers = [ - "54123", - "54123", - "54123", - "54456", - "54789", - "54789", -] +phone_numbers = ["54123", "54123", "54456", "54789", "54789", "54123"] phone_numbers = list(dict.fromkeys(phone_numbers)) print(phone_numbers) -phone_numbers = [ - "54123", - "54123", - "54123", - "54456", - "54789", - "54789", -] -set(phone_numbers) + +phone_numbers = ["54123", "54123", "54456", "54789", "54789", "54123"] +phone_numbers = set(phone_numbers) print(phone_numbers)