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..4e7c01c880 --- /dev/null +++ b/python-dicts/configs.py @@ -0,0 +1,62 @@ +config = { + "color": "green", + "width": 42, + "height": 100, + "font": "Courier", +} + +# Access a value through its key +print(config["color"]) + +# Update a value +config["font"] = "Helvetica" +print(config) + +config = { + "color": "green", + "width": 42, + "height": 100, + "font": "Courier", +} +user_config = { + "path": "/home", + "color": "red", + "font": "Arial", + "position": (200, 100), +} +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_config = { + "color": "green", + "width": 42, + "height": 100, + "font": "Courier", +} +user_config = { + "path": "/home", + "color": "red", + "font": "Arial", + "position": (200, 100), +} +config = default_config | user_config +print(config) + +config = { + "color": "green", + "width": 42, + "height": 100, + "font": "Courier", +} +user_config = { + "path": "/home", + "color": "red", + "font": "Arial", + "position": (200, 100), +} +config |= user_config +print(config) 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..82751e201b --- /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), + } +)