Creating a dictionary with contents corresponding to the list from among the dictionary

Viewed 38
a = {'Goal': 907.89,'Book': 64.03, 'Tube': 34.12, 'Coke': 153.62, 'Note': 312.86, 'Web':43.21,'Sign':8.02}
b = {'Book', 'Coke', 'Web', 'Lime'}

for key in a :
    if key in b :
        print(key)
    else :
        print()

There is a dictionary and a list. I want to complete a dictionary based on the common keys of a dictionary and a list. I know how to find the keys, but it's difficult to create a dictionary with keys and values. I would really appreciate it if you could teach me how. thank you.

The result I want is this.

{'Book' : 64.03, 'Coke' : 153.62, 'Web':43.21}
1 Answers
a = {'Goal': 907.89,'Book': 64.03, 'Tube': 34.12, 'Coke': 153.62, 'Note': 312.86, 'Web':43.21,'Sign':8.02}
b = {'Book', 'Coke', 'Web', 'Lime'}

print( {k: a[k] for k in a.keys() & b} )

Prints:

{'Book': 64.03, 'Coke': 153.62, 'Web': 43.21}

Note: b isn't of type list, it's a set

Related