Home > Blockchain >  Check if a List Element Exists in Dictionary Values
Check if a List Element Exists in Dictionary Values

Time:11-08

I have a list and a dictionary, and I need to check if elements of the list exist in the dictionary. If yes, I need to copy the key-value pair to a new dictionary, and discard the rest from the dictionary.

list0 = [
    -0.2385384148158066,
    -0.2307061452151448,
    -0.2150726345799602,
    -0.2138622652947372,
    -0.1098235264547504,
    -0.1072424768342444,
    -0.1037212327115436,
    -0.0966926943378552,
    -0.09614853460521154,
    9.353161396230564e-07
]
dict0 = {
    67: 1.440192237446893e-05,
    91: -0.1037212327115436,
    115: -0.2307061452151448,
    172: 0.0002879308975510175,
    242: 1.340170610273099e-05,
    266: -0.09614853460521154,
    290: -0.2138622652947372,
    347: 9.353161396230564e-07,
    417: 1.462739691486375e-05,
    441: -0.1072424768342444,
    465: -0.2385384148158066,
    513: 0.001208308075300354,
    606: -0.1098235264547504,
    630: -0.2442794187837403,
    677: 0.0007379497093922571,
    747: 1.336163063745514e-05,
    771: -0.0966926943378552,
    794: -0.2150726345799602
}

I tried iterating over the list and checking if the element is in dict0.values() and vice versa, but still couldn't figure out any way to access both key and value.

CodePudding user response:

You can use a dict comprehension:

>>> {k: v for k, v in dict0.items() if v in list0}
{91: -0.1037212327115436, 115: -0.2307061452151448, 266: -0.09614853460521154, 290: -0.2138622652947372, 347: 9.353161396230564e-07, 441: -0.1072424768342444, 465: -0.2385384148158066, 606: -0.1098235264547504, 771: -0.0966926943378552, 794: -0.2150726345799602}

If your list0 is very big, converting it to a set makes the above method faster:

>>> set0 = set(list0)
>>> {k: v for k, v in dict0.items() if v in set0}

CodePudding user response:

Thats Pretty Simple to do:

This snippet may help:

new_dict={}
    for item in list0:
       for d in dict0:
          if(item==dict0[d]):
             new_dict[d]=item

CodePudding user response:

I'm not able to figure out why didn't you get the answer by iteration. You've not pasted your code, so I'm giving the answer in iteration method only so that you can compare it with your code. The code would be:

list0 = [
    -0.2385384148158066,
    -0.2307061452151448,
    -0.2150726345799602,
    -0.2138622652947372,
    -0.1098235264547504,
    -0.1072424768342444,
    -0.1037212327115436,
    -0.0966926943378552,
    -0.09614853460521154,
    9.353161396230564e-07
]
dict0 = {
    67: 1.440192237446893e-05,
    91: -0.1037212327115436,
    115: -0.2307061452151448,
    172: 0.0002879308975510175,
    242: 1.340170610273099e-05,
    266: -0.09614853460521154,
    290: -0.2138622652947372,
    347: 9.353161396230564e-07,
    417: 1.462739691486375e-05,
    441: -0.1072424768342444,
    465: -0.2385384148158066,
    513: 0.001208308075300354,
    606: -0.1098235264547504,
    630: -0.2442794187837403,
    677: 0.0007379497093922571,
    747: 1.336163063745514e-05,
    771: -0.0966926943378552,
    794: -0.2150726345799602
}
new_dict={}
for i in dict0:
    if dict0[i] in list0:
        new_dict[i]=dict0[i]
print(new_dict)
  • Related