Skip to content

Commit ca7b0bb

Browse files
authored
Merge pull request #614 from realpython/python-dicts
Sample code for the article on dictionaries
2 parents bd032b2 + 0f93296 commit ca7b0bb

File tree

17 files changed

+300
-0
lines changed

17 files changed

+300
-0
lines changed

python-dicts/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Dictionaries in Python
2+
3+
This folder provides the code examples for the Real Python tutorial [Dictionaries in Python](https://realpython.com/python-dicts/).

python-dicts/configs.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
config = {
2+
"color": "green",
3+
"width": 42,
4+
"height": 100,
5+
"font": "Courier",
6+
}
7+
8+
# Access a value through its key
9+
print(config["color"])
10+
11+
# Update a value
12+
config["font"] = "Helvetica"
13+
print(config)
14+
15+
config = {
16+
"color": "green",
17+
"width": 42,
18+
"height": 100,
19+
"font": "Courier",
20+
}
21+
user_config = {
22+
"path": "/home",
23+
"color": "red",
24+
"font": "Arial",
25+
"position": (200, 100),
26+
}
27+
config.update(user_config)
28+
print(config)
29+
config.update([("width", 200), ("api_key", 1234)])
30+
print(config)
31+
config.update(color="yellow", script="__main__.py")
32+
print(config)
33+
34+
default_config = {
35+
"color": "green",
36+
"width": 42,
37+
"height": 100,
38+
"font": "Courier",
39+
}
40+
user_config = {
41+
"path": "/home",
42+
"color": "red",
43+
"font": "Arial",
44+
"position": (200, 100),
45+
}
46+
config = default_config | user_config
47+
print(config)
48+
49+
config = {
50+
"color": "green",
51+
"width": 42,
52+
"height": 100,
53+
"font": "Courier",
54+
}
55+
user_config = {
56+
"path": "/home",
57+
"color": "red",
58+
"font": "Arial",
59+
"position": (200, 100),
60+
}
61+
config |= user_config
62+
print(config)

python-dicts/counter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from collections import Counter
2+
3+
print(Counter("mississippi"))

python-dicts/dict_zip.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cities = ["Colorado", "Chicago", "Boston", "Minnesota", "Milwaukee", "Seattle"]
2+
teams = ["Rockies", "White Sox", "Red Sox", "Twins", "Brewers", "Mariners"]
3+
4+
print(dict(zip(cities, teams)))

python-dicts/employees.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from collections import defaultdict
2+
3+
employees = [
4+
("Sales", "John"),
5+
("Sales", "Martin"),
6+
("Accounting", "Kate"),
7+
("Marketing", "Elizabeth"),
8+
("Marketing", "Linda"),
9+
]
10+
11+
departments = defaultdict(list)
12+
for department, employee in employees:
13+
departments[department].append(employee)
14+
15+
print(departments)

python-dicts/equality.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
print([1, 2, 3] == [3, 2, 1])
2+
print({1: 1, 2: 2, 3: 3} == {3: 3, 2: 2, 1: 1})
3+
print([1, 2, 3] != [3, 2, 1])
4+
print({1: 1, 2: 2, 3: 3} != {3: 3, 2: 2, 1: 1})

python-dicts/from_keys.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
inventory = dict.fromkeys(["apple", "orange", "banana", "mango"], 0)
2+
print(inventory)

python-dicts/inventory.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
inventory = {"apple": 100, "orange": 80, "banana": 100}
2+
inventory.get("apple")
3+
4+
print(inventory.get("mango"))
5+
6+
print(inventory.get("mango", 0))
7+
8+
print(inventory.values())
9+
10+
print(inventory.keys())
11+
12+
print(inventory.items())

python-dicts/iteration.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
students = {
2+
"Alice": 89.5,
3+
"Bob": 76.0,
4+
"Charlie": 92.3,
5+
"Diana": 84.7,
6+
"Ethan": 88.9,
7+
"Fiona": 95.6,
8+
"George": 73.4,
9+
"Hannah": 81.2,
10+
}
11+
for student in students:
12+
print(student)
13+
14+
for student in students.keys():
15+
print(student)
16+
17+
for student in students:
18+
print(student, "->", students[student])
19+
20+
MLB_teams = {
21+
"Colorado": "Rockies",
22+
"Chicago": "White Sox",
23+
"Boston": "Red Sox",
24+
"Minnesota": "Twins",
25+
"Milwaukee": "Brewers",
26+
"Seattle": "Mariners",
27+
}
28+
for team in MLB_teams.values():
29+
print(team)
30+
31+
for city, team in MLB_teams.items():
32+
print(city, "->", team)

python-dicts/membership.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import timeit
2+
3+
MLB_teams = {
4+
"Colorado": "Rockies",
5+
"Chicago": "White Sox",
6+
"Boston": "Red Sox",
7+
"Minnesota": "Twins",
8+
"Milwaukee": "Brewers",
9+
"Seattle": "Mariners",
10+
}
11+
12+
# Run timeit to compare the membership test
13+
time_in_dict = timeit.timeit(
14+
'"Milwaukee" in MLB_teams', globals=globals(), number=1000000
15+
)
16+
time_in_keys = timeit.timeit(
17+
'"Milwaukee" in MLB_teams.keys()', globals=globals(), number=1000000
18+
)
19+
time_not_in_dict = timeit.timeit(
20+
'"Indianapolis" in MLB_teams', globals=globals(), number=1000000
21+
)
22+
time_not_in_keys = timeit.timeit(
23+
'"Indianapolis" in MLB_teams.keys()', globals=globals(), number=1000000
24+
)
25+
26+
print(
27+
f"{time_in_dict = } seconds",
28+
f"{time_in_keys = } seconds",
29+
f"{time_not_in_dict = } seconds",
30+
f"{time_not_in_keys = } seconds",
31+
sep="\n",
32+
)

0 commit comments

Comments
 (0)