-
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 #597 from realpython/python-dict-comprehension
Sample code for the article on dict comprehension
- Loading branch information
Showing
15 changed files
with
97 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 @@ | ||
# 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/). |
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,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)}) |
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,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) | ||
} | ||
) |
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 @@ | ||
print(dict(apple=0.40, orange=0.35, banana=0.25)) |
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,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"}) |
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,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}) |
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,2 @@ | ||
fruits = ["apple", "banana", "cherry"] | ||
print({fruit.upper(): len(fruit) for fruit in fruits}) |
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,6 @@ | ||
fruits = { | ||
"apple": 1.00, | ||
"banana": 0.50, | ||
"cherry": 2.00, | ||
} | ||
print({fruit: round(price * 0.95, 2) for fruit, price in fruits.items()}) |
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,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() | ||
} | ||
) |
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 @@ | ||
from string import ascii_lowercase | ||
|
||
print({letter: ord(letter) for letter in ascii_lowercase}) |
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,4 @@ | ||
likes = {"color": "blue", "fruit": "apple", "pet": "dog"} | ||
print(likes) | ||
likes["hobby"] = "guitar" | ||
print(likes) |
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,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}) |
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,6 @@ | ||
powers_of_two = {} | ||
|
||
for integer in range(1, 10): | ||
powers_of_two[integer] = 2**integer | ||
|
||
print(powers_of_two) |
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,2 @@ | ||
power_of_two = {integer: 2**integer for integer in range(1, 10)} | ||
print(power_of_two) |
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,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()}) |