Skip to content

Commit f9e63d5

Browse files
Merge branch 'master' into python-for-loop
2 parents eceb1b4 + 6301b57 commit f9e63d5

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Remove Items From Lists in Python
2+
3+
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/).
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"]
2+
print(books.pop(0))
3+
print(books)
4+
5+
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"]
6+
read_books = []
7+
read = books.pop(0)
8+
read_books.append(read)
9+
print(read_books)
10+
print(books)
11+
12+
books = ["Dragonsbane", "The Hobbit", "Wonder", "Wonder", "Jaws", "Jaws"]
13+
del books[2]
14+
print(books)
15+
del books[-1]
16+
print(books)
17+
18+
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"]
19+
books.remove("The Hobbit")
20+
print(books)
21+
22+
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"]
23+
books.remove("The Two Towers")
24+
print(books)
25+
26+
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws"]
27+
del books[0:3]
28+
print(books)
29+
30+
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws", "It"]
31+
del books[-3:-1]
32+
print(books)
33+
34+
books = ["Dragonsbane", "The Hobbit", "Wonder", "Jaws", "It"]
35+
books.clear()
36+
print(books)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
phone_numbers = [
2+
"54123",
3+
"54123",
4+
"54123",
5+
"54456",
6+
"54789",
7+
"54789",
8+
]
9+
for phone_number in phone_numbers[:]:
10+
if phone_numbers.count(phone_number) > 1:
11+
phone_numbers.remove(phone_number)
12+
print(phone_numbers)
13+
14+
15+
phone_numbers = [
16+
"54123",
17+
"54123",
18+
"54123",
19+
"54456",
20+
"54789",
21+
"54789",
22+
]
23+
phone_numbers = list(dict.fromkeys(phone_numbers))
24+
print(phone_numbers)
25+
26+
phone_numbers = [
27+
"54123",
28+
"54123",
29+
"54123",
30+
"54456",
31+
"54789",
32+
"54789",
33+
]
34+
set(phone_numbers)
35+
print(phone_numbers)

python-get-all-files-in-directory/create_large_dir.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ class Item:
6666

6767
def create_item(item: Item, path_to: Path = Path.cwd()) -> None:
6868

69-
if not item.children and not item.junk_files:
70-
path_to.joinpath(item.name).touch()
69+
if not item.children and not item.num_junk_files:
70+
path_to.joinpath(item.name).touch(exist_ok=True)
7171
return
7272

7373
root = path_to.joinpath(item.name)
74-
root.mkdir()
74+
root.mkdir(exist_ok=True)
7575

7676
for child in item.children:
7777
create_item(child, path_to=root)
7878

7979
for i in range(item.num_junk_files):
80-
root.joinpath(f"{i}.txt").touch()
80+
root.joinpath(f"{i}.txt").touch(exist_ok=True)
8181

8282

8383
create_item(folder_structure)

0 commit comments

Comments
 (0)