DEV Community

Discussion on: Python for JavaScript Developers

 
cben profile image
Beni Cherniavsky-Paskin

But note that in python for key in {1: 10, 2: 20}: iterates over dictionary's keys.

You can use for value in d.keys():, for (key, value) in d.items(): and (redundant) for k in d.keys():.
These methods exist in both Python 2 and 3, though with some differences (see python.org/dev/peps/pep-3106/).

Thread Thread
 
tomleo profile image
Tom Leo

You can drop the parenthesis i.e. for k, v in thing.items(). You only need parenthesis for comprehension i.e. {k: v for (k,v) in thing.items()} or [(k,v) for (k,v) in thing.items()]