dict1 = {'1':'foo', '2':'bar', '3':'spam','4':'bar'}dict2 = {} for k, v in dict1.iteritems(): print dict2.keys(), v if v not in dict2.keys(): print "adding %s to dict2" % v dict2[v] = "whatever" else: print "Repeated: ",v print dict2
Standard input is empty
[] foo adding foo to dict2 ['foo'] spam adding spam to dict2 ['foo', 'spam'] bar adding bar to dict2 ['foo', 'bar', 'spam'] bar Repeated: bar {'foo': 'whatever', 'bar': 'whatever', 'spam': 'whatever'}
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!