How to compare list and dictionary in Python???

All other Source.Python topics and issues.
User avatar
pikkip
Junior Member
Posts: 7
Joined: Mon Oct 17, 2016 11:38 am

How to compare list and dictionary in Python???

Postby pikkip » Fri Nov 18, 2016 11:24 am

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:
User avatar
Ayuto
Project Leader
Posts: 1587
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

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

Postby Ayuto » Fri Nov 18, 2016 11:53 am

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)
User avatar
pikkip
Junior Member
Posts: 7
Joined: Mon Oct 17, 2016 11:38 am

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

Postby pikkip » Fri Nov 18, 2016 11:55 am

Thank you

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest