-1

I have a dictionary values and list of list values. I need to compare the list values with the dictionary values and get the matched data with key and value pairs.

My dictionary:

res = {
    'cXboTHyIeZaof6x7': ['#de6262', '#ffb88c'],
    '19hyHnlzDMEDJ9N5': ['#ffcc66', '#FFFFFF'],
    'TByXB1YzYSJW2kXO': ['#7A7FBC', '#807CC4'],
    'utTtWdchE2T6vUF5': ['#A3DAC3', '#8BD0D4']
}

My list of list values:

diff_tolistoflist = [
    ['#de6262', '#ffb88c'],
    ['#A3DAC3', '#8BD0D4']
]

Expected output:

test = {
    'cXboTHyIeZaof6x7': ['#de6262', '#ffb88c'],
    'utTtWdchE2T6vUF5': ['#A3DAC3', '#8BD0D4']
}

I need to compare dict values and list of values to get the similar data with key and value pair.

accdias
  • 5,160
  • 3
  • 19
  • 31
sangeetha
  • 53
  • 1
  • 9
  • 1
    welcome to stackoverflow! please take the [tour](http://stackoverflow.com/tour), read up on [how to ask a question](https://stackoverflow.com/help/asking) and provide the [shortest program necessary to reproduce the problem](https://stackoverflow.com/help/minimal-reproducible-example). also make sure to provide some input and the corresponding desired output. – hiro protagonist Nov 04 '19 at 10:39
  • 1
    This should answer your question: [Removing entries from a dictionary based on values](https://stackoverflow.com/questions/15158599/removing-entries-from-a-dictionary-based-on-values) – Georgy Nov 04 '19 at 10:55
  • This is not my question. This is to take difference between dictionary having list values and list of another values – sangeetha Nov 04 '19 at 11:03
  • Checking if an item is in a list is a fairly basic thing to do: [Fastest way to check if a value exists in a list](https://stackoverflow.com/q/7571635/7851470). And combining this check with the code in the duplicate target should be trivial. – Georgy Nov 04 '19 at 11:19

4 Answers4

2

You can use a dictionary comprehension which is quite easy to read:

d = {k:v for k,v in res.items() if v in diff_tolistoflist}
print(d)  
# {'cXboTHyIeZaof6x7': ['#de6262', '#ffb88c'], 'utTtWdchE2T6vUF5': ['#A3DAC3', '#8BD0D4']}
Laurent H.
  • 6,316
  • 1
  • 18
  • 40
0

You can check the the list item with dict val_list

test = {}
for key, val in res.items():
    if val in diff_tolistoflist:
        test[key] = val

Result

>>> test
>>> {'cXboTHyIeZaof6x7': ['#de6262', '#ffb88c'], 'utTtWdchE2T6vUF5': ['#A3DAC3', '#8BD0D4']}
Saleem Ali
  • 1,363
  • 11
  • 21
0

If the values are unique, we can invert our dictionary:

reversed_dict = {tuple(res[key]): key for key in res}

Keys in the dictionary need to be mutable, so we had to convert them to tuples.

Then we can do a simple lookup of values:

final_result = {}
for v in diff_tolistoflist:
    k = reversed_dict.get(tuple(v))
    if k:
        final_result[k] = v

I've used dict.get so that it doesn't throw an exception if something's wrong.

>>> final_result
{'cXboTHyIeZaof6x7': ['#de6262', '#ffb88c'], 'utTtWdchE2T6vUF5': ['#A3DAC3', '#8BD0D4']}

This solution is case-sensitive. That means, f and F are considered different. To make it case-insensitive, you can convert string to lower when reversing the dictionary and later do the same with the diff_tolistoflist (when declaring it or when looking up the values in the loop).

h4z3
  • 5,265
  • 1
  • 15
  • 29
0

Try this, I've user Filter function.

print dict(filter(lambda x:x[1] in diff_tolistoflist,res.items()))

Result:{'cXboTHyIeZaof6x7': ['#de6262', '#ffb88c'], 'utTtWdchE2T6vUF5': ['#A3DAC3', '#8BD0D4']}
Narendra Lucky
  • 340
  • 2
  • 13