Page 1 of 1

How to compare list and dictionary in Python???

Posted: Fri Nov 18, 2016 11:24 am
by pikkip
dictionary = {0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
list1 = [zero, one, two]

I want to delete the entry '3:three' from the dictionary since it is not in the list. I am retaining only the common entries in the list. How can I do it??? :confused:

Re: How to compare list and dictionary in Python???

Posted: Fri Nov 18, 2016 11:53 am
by Ayuto

Syntax: Select all

dictionary = {0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
list1 = ['zero', 'one', 'two']

for key, value in tuple(dictionary.items()):
if value not in list1:
del dictionary[key]

print(dictionary)

Re: How to compare list and dictionary in Python???

Posted: Fri Nov 18, 2016 11:55 am
by pikkip
Thank you