From a5bab76633f39c5326b63c500e4cd5e0899fa308 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Wed, 20 Nov 2024 19:48:47 +0100 Subject: [PATCH 1/3] Sample code for the article on dictionaries --- python-dicts/README.md | 3 ++ python-dicts/configs.py | 62 +++++++++++++++++++++++++++++++++++++ python-dicts/counter.py | 3 ++ python-dicts/dict_zip.py | 4 +++ python-dicts/employees.py | 15 +++++++++ python-dicts/equality.py | 4 +++ python-dicts/from_keys.py | 2 ++ python-dicts/inventory.py | 12 +++++++ python-dicts/iteration.py | 32 +++++++++++++++++++ python-dicts/membership.py | 32 +++++++++++++++++++ python-dicts/mlb_teams.py | 33 ++++++++++++++++++++ python-dicts/number.py | 6 ++++ python-dicts/person.py | 21 +++++++++++++ python-dicts/sorted_dict.py | 35 +++++++++++++++++++++ python-dicts/squares.py | 10 ++++++ python-dicts/students.py | 12 +++++++ python-dicts/values.py | 14 +++++++++ 17 files changed, 300 insertions(+) create mode 100644 python-dicts/README.md create mode 100644 python-dicts/configs.py create mode 100644 python-dicts/counter.py create mode 100644 python-dicts/dict_zip.py create mode 100644 python-dicts/employees.py create mode 100644 python-dicts/equality.py create mode 100644 python-dicts/from_keys.py create mode 100644 python-dicts/inventory.py create mode 100644 python-dicts/iteration.py create mode 100644 python-dicts/membership.py create mode 100644 python-dicts/mlb_teams.py create mode 100644 python-dicts/number.py create mode 100644 python-dicts/person.py create mode 100644 python-dicts/sorted_dict.py create mode 100644 python-dicts/squares.py create mode 100644 python-dicts/students.py create mode 100644 python-dicts/values.py diff --git a/python-dicts/README.md b/python-dicts/README.md new file mode 100644 index 0000000000..ac4a108380 --- /dev/null +++ b/python-dicts/README.md @@ -0,0 +1,3 @@ +# Dictionaries in Python + +This folder provides the code examples for the Real Python tutorial [Dictionaries in Python](https://realpython.com/python-dicts/). diff --git a/python-dicts/configs.py b/python-dicts/configs.py new file mode 100644 index 0000000000..413129808d --- /dev/null +++ b/python-dicts/configs.py @@ -0,0 +1,62 @@ +configs = { + "color": "green", + "width": 42, + "height": 100, + "font": "Courier", +} + +# Access a value through its key +print(configs["color"]) + +# Update a value +configs["font"] = "Helvetica" +print(configs) + +configs = { + "color": "green", + "width": 42, + "height": 100, + "font": "Courier", +} +user_configs = { + "path": "/home", + "color": "red", + "font": "Arial", + "position": (200, 100), +} +configs.update(user_configs) +print(configs) +configs.update([("width", 200), ("api_key", 1234)]) +print(configs) +configs.update(color="yellow", script="__main__.py") +print(configs) + +default_configs = { + "color": "green", + "width": 42, + "height": 100, + "font": "Courier", +} +user_configs = { + "path": "/home", + "color": "red", + "font": "Arial", + "position": (200, 100), +} +configs = default_configs | user_configs +print(configs) + +configs = { + "color": "green", + "width": 42, + "height": 100, + "font": "Courier", +} +user_configs = { + "path": "/home", + "color": "red", + "font": "Arial", + "position": (200, 100), +} +configs |= user_configs +print(configs) diff --git a/python-dicts/counter.py b/python-dicts/counter.py new file mode 100644 index 0000000000..511cac6f08 --- /dev/null +++ b/python-dicts/counter.py @@ -0,0 +1,3 @@ +from collections import Counter + +print(Counter("mississippi")) diff --git a/python-dicts/dict_zip.py b/python-dicts/dict_zip.py new file mode 100644 index 0000000000..6038d67fdc --- /dev/null +++ b/python-dicts/dict_zip.py @@ -0,0 +1,4 @@ +cities = ["Colorado", "Chicago", "Boston", "Minnesota", "Milwaukee", "Seattle"] +teams = ["Rockies", "White Sox", "Red Sox", "Twins", "Brewers", "Mariners"] + +print(dict(zip(cities, teams))) diff --git a/python-dicts/employees.py b/python-dicts/employees.py new file mode 100644 index 0000000000..6671bc6390 --- /dev/null +++ b/python-dicts/employees.py @@ -0,0 +1,15 @@ +from collections import defaultdict + +employees = [ + ("Sales", "John"), + ("Sales", "Martin"), + ("Accounting", "Kate"), + ("Marketing", "Elizabeth"), + ("Marketing", "Linda"), +] + +departments = defaultdict(list) +for department, employee in employees: + departments[department].append(employee) + +print(departments) diff --git a/python-dicts/equality.py b/python-dicts/equality.py new file mode 100644 index 0000000000..822bceba5b --- /dev/null +++ b/python-dicts/equality.py @@ -0,0 +1,4 @@ +print([1, 2, 3] == [3, 2, 1]) +print({1: 1, 2: 2, 3: 3} == {3: 3, 2: 2, 1: 1}) +print([1, 2, 3] != [3, 2, 1]) +print({1: 1, 2: 2, 3: 3} != {3: 3, 2: 2, 1: 1}) diff --git a/python-dicts/from_keys.py b/python-dicts/from_keys.py new file mode 100644 index 0000000000..ab7815163b --- /dev/null +++ b/python-dicts/from_keys.py @@ -0,0 +1,2 @@ +inventory = dict.fromkeys(["apple", "orange", "banana", "mango"], 0) +print(inventory) diff --git a/python-dicts/inventory.py b/python-dicts/inventory.py new file mode 100644 index 0000000000..dc58767e33 --- /dev/null +++ b/python-dicts/inventory.py @@ -0,0 +1,12 @@ +inventory = {"apple": 100, "orange": 80, "banana": 100} +inventory.get("apple") + +print(inventory.get("mango")) + +print(inventory.get("mango", 0)) + +print(inventory.values()) + +print(inventory.keys()) + +print(inventory.items()) diff --git a/python-dicts/iteration.py b/python-dicts/iteration.py new file mode 100644 index 0000000000..27c1ebe1a5 --- /dev/null +++ b/python-dicts/iteration.py @@ -0,0 +1,32 @@ +students = { + "Alice": 89.5, + "Bob": 76.0, + "Charlie": 92.3, + "Diana": 84.7, + "Ethan": 88.9, + "Fiona": 95.6, + "George": 73.4, + "Hannah": 81.2, +} +for student in students: + print(student) + +for student in students.keys(): + print(student) + +for student in students: + print(student, "->", students[student]) + +MLB_teams = { + "Colorado": "Rockies", + "Chicago": "White Sox", + "Boston": "Red Sox", + "Minnesota": "Twins", + "Milwaukee": "Brewers", + "Seattle": "Mariners", +} +for team in MLB_teams.values(): + print(team) + +for city, team in MLB_teams.items(): + print(city, "->", team) diff --git a/python-dicts/membership.py b/python-dicts/membership.py new file mode 100644 index 0000000000..b3c1d6fc0e --- /dev/null +++ b/python-dicts/membership.py @@ -0,0 +1,32 @@ +import timeit + +MLB_teams = { + "Colorado": "Rockies", + "Chicago": "White Sox", + "Boston": "Red Sox", + "Minnesota": "Twins", + "Milwaukee": "Brewers", + "Seattle": "Mariners", +} + +# Run timeit to compare the membership test +time_in_dict = timeit.timeit( + '"Milwaukee" in MLB_teams', globals=globals(), number=1000000 +) +time_in_keys = timeit.timeit( + '"Milwaukee" in MLB_teams.keys()', globals=globals(), number=1000000 +) +time_not_in_dict = timeit.timeit( + '"Indianapolis" in MLB_teams', globals=globals(), number=1000000 +) +time_not_in_keys = timeit.timeit( + '"Indianapolis" in MLB_teams.keys()', globals=globals(), number=1000000 +) + +print( + f"{time_in_dict = } seconds", + f"{time_in_keys = } seconds", + f"{time_not_in_dict = } seconds", + f"{time_not_in_keys = } seconds", + sep="\n", +) diff --git a/python-dicts/mlb_teams.py b/python-dicts/mlb_teams.py new file mode 100644 index 0000000000..05011f39dc --- /dev/null +++ b/python-dicts/mlb_teams.py @@ -0,0 +1,33 @@ +MLB_teams = { + "Colorado": "Rockies", + "Chicago": "White Sox", + "Chicago": "Chicago Cubs", + "Boston": "Red Sox", + "Minnesota": "Twins", + "Milwaukee": "Brewers", + "Seattle": "Mariners", +} +print(MLB_teams) + +MLB_teams = dict( + [ + ("Colorado", "Rockies"), + ("Chicago", "White Sox"), + ("Boston", "Red Sox"), + ("Minnesota", "Twins"), + ("Milwaukee", "Brewers"), + ("Seattle", "Mariners"), + ] +) +print(MLB_teams) + + +print("Milwaukee" in MLB_teams) +print("Indianapolis" in MLB_teams) +print("Indianapolis" not in MLB_teams) +print("Milwaukee" in MLB_teams.keys()) +print("Indianapolis" in MLB_teams.keys()) +print("Indianapolis" not in MLB_teams.keys()) + +print(("Boston", "Red Sox") in MLB_teams.items()) +print(("Boston", "Red Sox") not in MLB_teams.items()) diff --git a/python-dicts/number.py b/python-dicts/number.py new file mode 100644 index 0000000000..204e6c17f6 --- /dev/null +++ b/python-dicts/number.py @@ -0,0 +1,6 @@ +class Number: + def __init__(self, value): + self.value = value + + +print(Number(42).__dict__) diff --git a/python-dicts/person.py b/python-dicts/person.py new file mode 100644 index 0000000000..3c28b64260 --- /dev/null +++ b/python-dicts/person.py @@ -0,0 +1,21 @@ +person = { + "first_name": "John", + "last_name": "Doe", + "age": 35, + "spouse": "Jane", + "children": ["Ralph", "Betty", "Bob"], + "pets": {"dog": "Frieda", "cat": "Sox"}, +} + +print(person["children"][0]) +print(person["children"][2]) +print(person["pets"]["dog"]) + +person = {} +person["first_name"] = "John" +person["last_name"] = "Doe" +person["age"] = 35 +person["spouse"] = "Jane" +person["children"] = ["Ralph", "Betty", "Bob"] +person["pets"] = {"dog": "Frieda", "cat": "Sox"} +print(person) diff --git a/python-dicts/sorted_dict.py b/python-dicts/sorted_dict.py new file mode 100644 index 0000000000..b6d4208042 --- /dev/null +++ b/python-dicts/sorted_dict.py @@ -0,0 +1,35 @@ +class SortableDict(dict): + def sort_by_keys(self, reverse=False): + sorted_items = sorted( + self.items(), key=lambda item: item[0], reverse=reverse + ) + self.clear() + self.update(sorted_items) + + def sort_by_values(self, reverse=False): + sorted_items = sorted( + self.items(), key=lambda item: item[1], reverse=reverse + ) + self.clear() + self.update(sorted_items) + + +students = SortableDict( + { + "Alice": 89.5, + "Bob": 76.0, + "Charlie": 92.3, + "Diana": 84.7, + "Ethan": 88.9, + "Fiona": 95.6, + "George": 73.4, + "Hannah": 81.2, + } +) + +print(id(students)) +students.sort_by_keys() +print(students) +students.sort_by_values(reverse=True) +print(students) +print(id(students)) diff --git a/python-dicts/squares.py b/python-dicts/squares.py new file mode 100644 index 0000000000..7a78da28af --- /dev/null +++ b/python-dicts/squares.py @@ -0,0 +1,10 @@ +squares = {} + +for integer in range(1, 10): + squares[integer] = integer**2 + +print(squares) + +squares = {integer: integer**2 for integer in range(1, 10)} + +print(squares) diff --git a/python-dicts/students.py b/python-dicts/students.py new file mode 100644 index 0000000000..5fdeba916d --- /dev/null +++ b/python-dicts/students.py @@ -0,0 +1,12 @@ +students = { + "Alice": 89.5, + "Bob": 76.0, + "Charlie": 92.3, + "Diana": 84.7, + "Ethan": 88.9, + "Fiona": 95.6, + "George": 73.4, + "Hannah": 81.2, +} +print(dict(sorted(students.items(), key=lambda item: item[1]))) +print(dict(sorted(students.items(), key=lambda item: item[1], reverse=True))) diff --git a/python-dicts/values.py b/python-dicts/values.py new file mode 100644 index 0000000000..c85a45c026 --- /dev/null +++ b/python-dicts/values.py @@ -0,0 +1,14 @@ +class Point: + def __init__(self, x, y): + self.x = x + self.y = y + + +print( + { + "colors": ["red", "green", "blue"], + "plugins": {"py_code", "dev_sugar", "fasting_py"}, + "timeout": 3, + "position": Point(42, 21), + } +) From 61aa67d3eedb5f4d6d0b2787dad3ea2c0936f667 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Wed, 20 Nov 2024 19:52:23 +0100 Subject: [PATCH 2/3] Comment repeated key --- python-dicts/mlb_teams.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-dicts/mlb_teams.py b/python-dicts/mlb_teams.py index 05011f39dc..82751e201b 100644 --- a/python-dicts/mlb_teams.py +++ b/python-dicts/mlb_teams.py @@ -1,6 +1,6 @@ MLB_teams = { "Colorado": "Rockies", - "Chicago": "White Sox", + # "Chicago": "White Sox", "Chicago": "Chicago Cubs", "Boston": "Red Sox", "Minnesota": "Twins", From 75c5f152efbe62d07ad33e9fb4712f2041d351d8 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Tue, 26 Nov 2024 16:11:41 +0100 Subject: [PATCH 3/3] TR updates, first round --- python-dicts/configs.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/python-dicts/configs.py b/python-dicts/configs.py index 413129808d..4e7c01c880 100644 --- a/python-dicts/configs.py +++ b/python-dicts/configs.py @@ -1,4 +1,4 @@ -configs = { +config = { "color": "green", "width": 42, "height": 100, @@ -6,57 +6,57 @@ } # Access a value through its key -print(configs["color"]) +print(config["color"]) # Update a value -configs["font"] = "Helvetica" -print(configs) +config["font"] = "Helvetica" +print(config) -configs = { +config = { "color": "green", "width": 42, "height": 100, "font": "Courier", } -user_configs = { +user_config = { "path": "/home", "color": "red", "font": "Arial", "position": (200, 100), } -configs.update(user_configs) -print(configs) -configs.update([("width", 200), ("api_key", 1234)]) -print(configs) -configs.update(color="yellow", script="__main__.py") -print(configs) +config.update(user_config) +print(config) +config.update([("width", 200), ("api_key", 1234)]) +print(config) +config.update(color="yellow", script="__main__.py") +print(config) -default_configs = { +default_config = { "color": "green", "width": 42, "height": 100, "font": "Courier", } -user_configs = { +user_config = { "path": "/home", "color": "red", "font": "Arial", "position": (200, 100), } -configs = default_configs | user_configs -print(configs) +config = default_config | user_config +print(config) -configs = { +config = { "color": "green", "width": 42, "height": 100, "font": "Courier", } -user_configs = { +user_config = { "path": "/home", "color": "red", "font": "Arial", "position": (200, 100), } -configs |= user_configs -print(configs) +config |= user_config +print(config)