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"] } +``` 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" +``` 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" ] +``` 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" } +``` 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 } +``` 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 } +```