Skip to content

Instantly share code, notes, and snippets.

@sidisinsane
Last active July 16, 2023 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidisinsane/a3ef9938a6c53bfb523ae7318f4be3e1 to your computer and use it in GitHub Desktop.
Save sidisinsane/a3ef9938a6c53bfb523ae7318f4be3e1 to your computer and use it in GitHub Desktop.
Return a dictionary containing unique values from a given dictionary.
def dict_unique(dict):
"""
Return a dictionary containing unique values from a given dictionary.
Args:
dict (dict): The input dictionary.
Returns:
dict: A new dictionary with unique values.
Example:
>>> d = {'a': 1, 'b': 2, 'c': 1, 'd': 3, 'e': 2}
>>> d_unique = dict_unique(d)
>>> print(d_unique)
{'a': 1, 'b': 2, 'd': 3}
"""
# Remove duplicate values
unique_values = list(set(dict.values()))
# Create a new dictionary with unique values
unique_dict = {key: value for key, value in dict.items() if value in unique_values}
return unique_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment