Last active
July 16, 2023 16:52
-
-
Save sidisinsane/a3ef9938a6c53bfb523ae7318f4be3e1 to your computer and use it in GitHub Desktop.
Return a dictionary containing unique values from a given dictionary.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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