From d3a354f697b0467dd1cf172820faa6761cc6bf1b Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Tue, 7 Apr 2020 21:13:32 +0300 Subject: [PATCH 1/6] Add invert_dictionary --- snippets/invert_dictionary.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/invert_dictionary.md diff --git a/snippets/invert_dictionary.md b/snippets/invert_dictionary.md new file mode 100644 index 000000000..b30ef9572 --- /dev/null +++ b/snippets/invert_dictionary.md @@ -0,0 +1,22 @@ +--- +title: invert_dictionary +tags: dictionary,intermediate +--- + +Inverts a dictionary with unique hashable values. + +Use `dictionary.items()` in combination with a list comprehension to create a new dictionary with the values and keys inverted. + +```py +def invert_dictionary(obj): + return { value: key for key, value in obj.items() } +``` + +```py +ages = { + "Peter": 10, + "Isabel": 11, + "Anna": 9, +} +invert_dictionary(ages) # { 10: "Peter", 11 "Isabel", 9: "Anna" } +``` From ecc35f200b52e239deea1eb796d10207472c5fca Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Tue, 7 Apr 2020 21:15:06 +0300 Subject: [PATCH 2/6] Add collect_dictionary --- snippets/collect_dictionary.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 snippets/collect_dictionary.md diff --git a/snippets/collect_dictionary.md b/snippets/collect_dictionary.md new file mode 100644 index 000000000..52ed1a9db --- /dev/null +++ b/snippets/collect_dictionary.md @@ -0,0 +1,25 @@ +--- +title: collect_dictionary +tags: dictionary,intermediate +--- + +Inverts a dictionary with non-unique hashable values. + +Use `dictionary.items()` in combination with a loop to map the values of the dictionary to keys using `dictionary.setdefault()`, `list()` and `append()` to create a list for each one. + +```py +def collect_dictionary(obj): + inv_obj = {} + for key, value in obj.items(): + inv_obj.setdefault(value, list()).append(key) + return inv_obj +``` + +```py +ages = { + "Peter": 10, + "Isabel": 10, + "Anna": 9, +} +collect_dictionary(ages) # { 10: ["Peter", "Isabel"], 9: ["Anna"] } +``` From 2e9de879b6be34ff3db918a8f8b8017323b72cb7 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Mon, 13 Apr 2020 19:30:29 +0300 Subject: [PATCH 3/6] Add to_dictionary --- snippets/to_dictionary.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/to_dictionary.md diff --git a/snippets/to_dictionary.md b/snippets/to_dictionary.md new file mode 100644 index 000000000..d99727872 --- /dev/null +++ b/snippets/to_dictionary.md @@ -0,0 +1,18 @@ +--- +title: to_dictionary +tags: list,dictionary,intermediate +--- + +Combines two lists into a dictionary, where the elements of the first one serve as the keys and the elements of the second one serve as the values. +The values of the first list need to be unique and hashable. + +Use `zip()` in combination with a list comprehension to combine the values of the two lists, based on their positions. + +```py +def to_dictionary(keys, values): + return {key:value for key, value in zip(keys, values)} +``` + +```py +to_dictionary(['a', 'b'], [1, 2]) # { a: 1, b: 2 } +``` From 4b0102d8caf2e53ed188839a065a02531602928c Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Thu, 16 Apr 2020 19:13:20 +0300 Subject: [PATCH 4/6] Add find_key --- snippets/find_key.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/find_key.md diff --git a/snippets/find_key.md b/snippets/find_key.md new file mode 100644 index 000000000..455666e34 --- /dev/null +++ b/snippets/find_key.md @@ -0,0 +1,22 @@ +--- +title: find_key +tags: dictionary,intermediate +--- + +Returns the first key in the provided dictionary that has the given value. + +Use `dictionary.items()` and `next()` to return the first key that has a value equal to `val`. + +```py +def find_key(dict, val): + return next(key for key, value in dict.items() if value == val) +``` + +```py +ages = { + "Peter": 10, + "Isabel": 11, + "Anna": 9, +} +find_key(ages, 11) # "Isabel" +``` From 195fcdef28c1877d76f282c61210de9d5b85e5f2 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Thu, 16 Apr 2020 19:17:13 +0300 Subject: [PATCH 5/6] Add find_keys --- snippets/find_keys.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/find_keys.md diff --git a/snippets/find_keys.md b/snippets/find_keys.md new file mode 100644 index 000000000..757c08d6a --- /dev/null +++ b/snippets/find_keys.md @@ -0,0 +1,22 @@ +--- +title: find_keys +tags: dictionary,intermediate +--- + +Returns all keys in the provided dictionary that have the given value. + +Use `dictionary.items()`, a generator and `list()` to return all keys that have a value equal to `val`. + +```py +def find_keys(dict, val): + return list(key for key, value in dict.items() if value == val) +``` + +```py +ages = { + "Peter": 10, + "Isabel": 11, + "Anna": 10, +} +find_keys(ages, 10) # [ "Peter", "Anna" ] +``` From 1bf7eb28c4eca269cdfb51a69a071933e29eb9b9 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Thu, 16 Apr 2020 19:28:35 +0300 Subject: [PATCH 6/6] Add merge_dictionaries --- snippets/merge_dictionaries.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 snippets/merge_dictionaries.md diff --git a/snippets/merge_dictionaries.md b/snippets/merge_dictionaries.md new file mode 100644 index 000000000..7a0ec9b63 --- /dev/null +++ b/snippets/merge_dictionaries.md @@ -0,0 +1,27 @@ +--- +title: merge_dictionaries +tags: dictionary,intermediate +--- + +Merges two or more dictionaries. + +Create a new `dict()` and loop over `dicts`, using `dictionary.update()` to add the key-value pairs from each one to the result. + +```py +def merge_dictionaries(*dicts): + res = dict() + for d in dicts: + res.update(d) + return res +``` + +```py +ages_one = { + "Peter": 10, + "Isabel": 11, +} +ages_two = { + "Anna": 9 +} +merge_dictionaries(ages_one, ages_two) # { "Peter": 10, "Isabel": 11, "Anna": 9 } +```