Return keys of dictionary if values match with a given list

maven

I want to return all the keys of a given dictionary, if the values inside the dictionary match to the elements of a list. Suppose I have the following dictionary:

my_dict = {'flower1': ['blue'], 
           'flower2': ['red', 'green', 'blue'],
           'flower3': ['yellow'],
           'flower4': ['blue', 'black', 'cyan']}

Now I want to match the values in the dictionary with the following elements in a list:

my_lst = ['black',
          'red',
          'blue',
          'yellow',
          'green',
          'purple',
          'brown',
          'cyan']

My goal is the get a dictionary like follows:

result_dict = {'black': ['flower4'], 
               'red': ['flower2'],
               'blue': ['flower1', 'flower2', 'flower4'],
               'yellow': ['flower3']
               'green': ['flower2'], 
               'purple': [],
               'brown': [],
               'cyan': []}

For now I have tried a simple list comprehension, which works fine, but returns just a simple and unordered list like:

In[14]: [key for key, value in my_dict.items() for i in range(0, len(my_lst)) if my_lst[i] in value]

Out[14]:['flower1',
         'flower2',
         'flower2',
         'flower2',
         'flower3',
         'flower4',
         'flower4',
         'flower4']

What is the best way to perform such an operation? I can't get my head around it, any help would be appreciated.

costaparas

Don't over-complicate it. Do it in two completely separate steps:

  1. Convert my_lst into a suitable result dictionary.
  2. Iterate through the flower/color data, and add them to the result dictionary.

For example:

# create result dict, adding each color as a key
# make the value of each key an empty list initially
result = {k: [] for k in my_lst}

# iterate through the items of the flower/color dict
for flower, colors in my_dict.items():

    # append the flower corresponding to each color
    # to the appropriate list in the result dict
    for color in colors:
        result[color].append(flower)

print(result)

Output:

{'black': ['flower4'], 'red': ['flower2'], 'blue': ['flower1', 'flower2', 'flower4'], 'yellow': ['flower3'], 'green': ['flower2'], 'purple': [], 'brown': [], 'cyan': ['flower4']}

This of course, assumes that each color in my_dict occurs in my_lst.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to match the keys of a dictionary and return their values?

Return a dictionary with keys for a given dictionary's values and vice versa

return dictionary keys with the same list values

How to retrieve values from nested dictionary given list of keys?

How to convert a dictionary to a list of keys, with repeat counts given by the values?

List to dictionary with keys and values

Match keys to values in dictionary in python

Search dictionary values and return List of keys meeting conditions

Python match dictionary in given list

python: return the exact match from list of values in a dictionary

Match dictionary keys and values in new dictionary

How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

Different list values for dictionary keys

Assign list values to dictionary keys

Dictionary Keys * values to a list in python

creating a function to return a dictionary with the keys as given divisors and the values are taken from a range

Creating a dictionary given values from a list and dictionary

How to match keys and values in a Python dictionary?

Check if any keys in a dictionary match any values

Use LINQ to match property values to Dictionary keys

Compare keys of dictionary with values of List and return all the matching values including duplicates

Return values of dictionary except for a given key

Python dictionary: Get list of values for list of keys

Swift: sort dictionary (by keys or by values) and return ordered array (of keys or values)

Construct a dictionary from a list of dictionary keys, sub-keys and values

Return dictionary keys based on list integer value

How to return dictionary keys as a list in Python?

Match Ansible Keys to values of other keys within a nested dictionary

How to check if dictionary keys match dictionary values in order