Skip to content

Instantly share code, notes, and snippets.

@sidisinsane
Last active July 16, 2023 16:52
Show Gist options
  • Select an option

  • Save sidisinsane/a3ef9938a6c53bfb523ae7318f4be3e1 to your computer and use it in GitHub Desktop.

Select an option

Save sidisinsane/a3ef9938a6c53bfb523ae7318f4be3e1 to your computer and use it in GitHub Desktop.

Revisions

  1. sidisinsane revised this gist Jul 16, 2023. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions dict_unique.py
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,10 @@ def dict_unique(dict):
    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}
    >>> 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()))
  2. sidisinsane created this gist Jul 16, 2023.
    24 changes: 24 additions & 0 deletions dict_unique.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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