Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code for the article on dict comprehension #597

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-dict-comprehension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python Dictionary Comprehensions: How and When to Use Them

This folder provides the code examples for the Real Python tutorial [Python Dictionary Comprehensions: How and When to Use Them](https://realpython.com/python-dictionary-comprehension/).
12 changes: 12 additions & 0 deletions python-dict-comprehension/computer_parts_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parts = [
"CPU",
"GPU",
"Motherboard",
"RAM",
"SSD",
"Power Supply",
"Case",
"Cooling Fan",
]
stocks = [15, 8, 12, 30, 25, 10, 5, 20]
print({part: stock for part, stock in zip(parts, stocks)})
18 changes: 18 additions & 0 deletions python-dict-comprehension/computer_parts_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
parts = [
"CPU",
"GPU",
"Motherboard",
"RAM",
"SSD",
"Power Supply",
"Case",
"Cooling Fan",
]
stocks = [15, 8, 12, 30, 25, 10, 5, 20]
part_costs = [250, 500, 150, 80, 100, 120, 70, 25]
print(
{
part: stock * cost
for part, stock, cost in zip(parts, stocks, part_costs)
}
)
1 change: 1 addition & 0 deletions python-dict-comprehension/contructor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print(dict(apple=0.40, orange=0.35, banana=0.25))
12 changes: 12 additions & 0 deletions python-dict-comprehension/filter_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
codes = {
"1001": "Townsville",
"1002": "Lakeview",
"1003": "Mountainview",
"1101": "Riverside",
"1102": "Hilltop",
"1201": "Greenfield",
"1202": "Sunnydale",
"1301": "Meadowbrook",
"1302": "Creekwood",
}
print({code: town for code, town in codes.items() if "1100" <= code <= "1300"})
2 changes: 2 additions & 0 deletions python-dict-comprehension/filter_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numbers = {"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}
print({key: value for key, value in numbers.items() if value % 2 == 0})
2 changes: 2 additions & 0 deletions python-dict-comprehension/fruits_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fruits = ["apple", "banana", "cherry"]
print({fruit.upper(): len(fruit) for fruit in fruits})
6 changes: 6 additions & 0 deletions python-dict-comprehension/fruits_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fruits = {
"apple": 1.00,
"banana": 0.50,
"cherry": 2.00,
}
print({fruit: round(price * 0.95, 2) for fruit, price in fruits.items()})
8 changes: 8 additions & 0 deletions python-dict-comprehension/fruits_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fruits = {"apple": 1.0, "banana": 0.5, "cherry": 2.0, "mango": 2.3}
with_discount = ["apple", "cherry"]
print(
{
fruit: price * 0.9 if fruit in with_discount else price
for fruit, price in fruits.items()
}
)
3 changes: 3 additions & 0 deletions python-dict-comprehension/letters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from string import ascii_lowercase

print({letter: ord(letter) for letter in ascii_lowercase})
4 changes: 4 additions & 0 deletions python-dict-comprehension/likes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
likes = {"color": "blue", "fruit": "apple", "pet": "dog"}
print(likes)
likes["hobby"] = "guitar"
print(likes)
7 changes: 7 additions & 0 deletions python-dict-comprehension/nested_for.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
print({value: value**2 for row in matrix for value in row})
6 changes: 6 additions & 0 deletions python-dict-comprehension/powers_of_two_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
powers_of_two = {}

for integer in range(1, 10):
powers_of_two[integer] = 2**integer

print(powers_of_two)
2 changes: 2 additions & 0 deletions python-dict-comprehension/powers_of_two_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
power_of_two = {integer: 2**integer for integer in range(1, 10)}
print(power_of_two)
11 changes: 11 additions & 0 deletions python-dict-comprehension/swap_key_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parts = {
"CPU": 10021,
"GPU": 10022,
"Motherboard": 10023,
"RAM": 10024,
"SSD": 10025,
"Power Supply": 10027,
"Case": 10026,
"Cooling Fan": 10025,
}
print({value: key for key, value in parts.items()})
Loading