From 662376ae708a087052a210aca4fc64e0b399adaf Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Tue, 15 Oct 2024 16:03:58 +0200 Subject: [PATCH 1/2] Sample code for the article on dict comprehension --- python-dict-comprehension/README.md | 3 +++ python-dict-comprehension/computer_parts_v1.py | 12 ++++++++++++ python-dict-comprehension/computer_parts_v2.py | 18 ++++++++++++++++++ python-dict-comprehension/contructor.py | 1 + python-dict-comprehension/filter_keys.py | 12 ++++++++++++ python-dict-comprehension/filter_values.py | 2 ++ python-dict-comprehension/fruits_v1.py | 2 ++ python-dict-comprehension/fruits_v2.py | 6 ++++++ python-dict-comprehension/fruits_v3.py | 8 ++++++++ python-dict-comprehension/letters.py | 3 +++ python-dict-comprehension/likes.py | 4 ++++ python-dict-comprehension/nested_for.py | 7 +++++++ python-dict-comprehension/powers_of_two_v1.py | 6 ++++++ python-dict-comprehension/powers_of_two_v2.py | 2 ++ python-dict-comprehension/swap_key_value.py | 11 +++++++++++ 15 files changed, 97 insertions(+) create mode 100644 python-dict-comprehension/README.md create mode 100644 python-dict-comprehension/computer_parts_v1.py create mode 100644 python-dict-comprehension/computer_parts_v2.py create mode 100644 python-dict-comprehension/contructor.py create mode 100644 python-dict-comprehension/filter_keys.py create mode 100644 python-dict-comprehension/filter_values.py create mode 100644 python-dict-comprehension/fruits_v1.py create mode 100644 python-dict-comprehension/fruits_v2.py create mode 100644 python-dict-comprehension/fruits_v3.py create mode 100644 python-dict-comprehension/letters.py create mode 100644 python-dict-comprehension/likes.py create mode 100644 python-dict-comprehension/nested_for.py create mode 100644 python-dict-comprehension/powers_of_two_v1.py create mode 100644 python-dict-comprehension/powers_of_two_v2.py create mode 100644 python-dict-comprehension/swap_key_value.py diff --git a/python-dict-comprehension/README.md b/python-dict-comprehension/README.md new file mode 100644 index 0000000000..b16cf8307f --- /dev/null +++ b/python-dict-comprehension/README.md @@ -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/). diff --git a/python-dict-comprehension/computer_parts_v1.py b/python-dict-comprehension/computer_parts_v1.py new file mode 100644 index 0000000000..a32f9dcd87 --- /dev/null +++ b/python-dict-comprehension/computer_parts_v1.py @@ -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)}) diff --git a/python-dict-comprehension/computer_parts_v2.py b/python-dict-comprehension/computer_parts_v2.py new file mode 100644 index 0000000000..4ecca57cb2 --- /dev/null +++ b/python-dict-comprehension/computer_parts_v2.py @@ -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) + } +) diff --git a/python-dict-comprehension/contructor.py b/python-dict-comprehension/contructor.py new file mode 100644 index 0000000000..7c8650d116 --- /dev/null +++ b/python-dict-comprehension/contructor.py @@ -0,0 +1 @@ +print(dict(apple=0.40, orange=0.35, banana=0.25)) diff --git a/python-dict-comprehension/filter_keys.py b/python-dict-comprehension/filter_keys.py new file mode 100644 index 0000000000..a379190a8a --- /dev/null +++ b/python-dict-comprehension/filter_keys.py @@ -0,0 +1,12 @@ +codes = { + "1001": "Townsville", + "1002": "Lakeview", + "1003": "Mountainview", + "1101": "Riverside", + "1102": "Hilltop", + "1201": "Greenfield", + "1202": "Sunnydale", + "1301": "Meadowbrook", + "1302": "Creekwood" +} +{code: town for code, town in codes.items() if "1100" <= code <= "1300"} diff --git a/python-dict-comprehension/filter_values.py b/python-dict-comprehension/filter_values.py new file mode 100644 index 0000000000..556c608655 --- /dev/null +++ b/python-dict-comprehension/filter_values.py @@ -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}) diff --git a/python-dict-comprehension/fruits_v1.py b/python-dict-comprehension/fruits_v1.py new file mode 100644 index 0000000000..6f28bb33c2 --- /dev/null +++ b/python-dict-comprehension/fruits_v1.py @@ -0,0 +1,2 @@ +fruits = ["apple", "banana", "cherry"] +print({fruit.upper(): len(fruit) for fruit in fruits}) diff --git a/python-dict-comprehension/fruits_v2.py b/python-dict-comprehension/fruits_v2.py new file mode 100644 index 0000000000..7451c298f6 --- /dev/null +++ b/python-dict-comprehension/fruits_v2.py @@ -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()}) diff --git a/python-dict-comprehension/fruits_v3.py b/python-dict-comprehension/fruits_v3.py new file mode 100644 index 0000000000..d38983637e --- /dev/null +++ b/python-dict-comprehension/fruits_v3.py @@ -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() + } +) diff --git a/python-dict-comprehension/letters.py b/python-dict-comprehension/letters.py new file mode 100644 index 0000000000..e2b39a005b --- /dev/null +++ b/python-dict-comprehension/letters.py @@ -0,0 +1,3 @@ +from string import ascii_lowercase + +print({letter: ord(letter) for letter in ascii_lowercase}) diff --git a/python-dict-comprehension/likes.py b/python-dict-comprehension/likes.py new file mode 100644 index 0000000000..8fbf73e4dc --- /dev/null +++ b/python-dict-comprehension/likes.py @@ -0,0 +1,4 @@ +likes = {"color": "blue", "fruit": "apple", "pet": "dog"} +print(likes) +likes["hobby"] = "guitar" +print(likes) diff --git a/python-dict-comprehension/nested_for.py b/python-dict-comprehension/nested_for.py new file mode 100644 index 0000000000..fb4e980fea --- /dev/null +++ b/python-dict-comprehension/nested_for.py @@ -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}) diff --git a/python-dict-comprehension/powers_of_two_v1.py b/python-dict-comprehension/powers_of_two_v1.py new file mode 100644 index 0000000000..a663156cc7 --- /dev/null +++ b/python-dict-comprehension/powers_of_two_v1.py @@ -0,0 +1,6 @@ +powers_of_two = {} + +for integer in range(1, 10): + powers_of_two[integer] = 2**integer + +print(powers_of_two) diff --git a/python-dict-comprehension/powers_of_two_v2.py b/python-dict-comprehension/powers_of_two_v2.py new file mode 100644 index 0000000000..beb7378849 --- /dev/null +++ b/python-dict-comprehension/powers_of_two_v2.py @@ -0,0 +1,2 @@ +power_of_two = {integer: 2**integer for integer in range(1, 10)} +print(power_of_two) diff --git a/python-dict-comprehension/swap_key_value.py b/python-dict-comprehension/swap_key_value.py new file mode 100644 index 0000000000..cdffd76247 --- /dev/null +++ b/python-dict-comprehension/swap_key_value.py @@ -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()}) From 6ac9abe8042a9f2f8ba10136bb48f252541af1a2 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Tue, 15 Oct 2024 16:32:40 +0200 Subject: [PATCH 2/2] Fix linter issues --- python-dict-comprehension/filter_keys.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python-dict-comprehension/filter_keys.py b/python-dict-comprehension/filter_keys.py index a379190a8a..3e8d6d078c 100644 --- a/python-dict-comprehension/filter_keys.py +++ b/python-dict-comprehension/filter_keys.py @@ -7,6 +7,6 @@ "1201": "Greenfield", "1202": "Sunnydale", "1301": "Meadowbrook", - "1302": "Creekwood" + "1302": "Creekwood", } -{code: town for code, town in codes.items() if "1100" <= code <= "1300"} +print({code: town for code, town in codes.items() if "1100" <= code <= "1300"})