Python Dictionary Snippets

The Python snippet collection contains helper functions for Python 3.6. It includes utilities for most common data types, such as primitivies, lists, dictionaries and date objects.

  • Understand Python's named tuples and start using them in your projects today.

  • collect_dictionary

    Python, Dictionary

    Inverts a dictionary with non-unique hashable values.

  • group_by

    Python, List

    Groups the elements of a list based on the given function.

  • sort_dict_by_value

    Python, Dictionary

    Sorts the given dictionary by value.

  • map_dictionary

    Python, List

    Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value.

  • get

    Python, Dictionary

    Retrieves the value of the nested key indicated by the given selector list from a dictionary or list.

  • invert_dictionary

    Python, Dictionary

    Inverts a dictionary with unique hashable values.

  • merge_dictionaries

    Python, Dictionary

    Merges two or more dictionaries.

  • Learn the difference between two common ways to access values in Python dictionaries and level up your code today.

  • pluck

    Python, List

    Converts a list of dictionaries into a list of values corresponding to the specified key.

  • sort_dict_by_key

    Python, Dictionary

    Sorts the given dictionary by key.

  • map_values

    Python, Dictionary

    Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value.

  • find_keys

    Python, Dictionary

    Finds all keys in the provided dictionary that have the given value.

  • to_dictionary

    Python, List

    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.

  • dict_to_list

    Python, Dictionary

    Converts a dictionary to a list of tuples.