Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get key from dictionary

I just started with Python,

Is there some iteration in dictionary like in PHP

foreach(aData as key=>value)
like image 325
OHLÁLÁ Avatar asked Dec 03 '22 02:12

OHLÁLÁ


1 Answers

It looks like something like this :

my_dict = {"key1": 1, "key2":2}
my_dict.items()       # in python < 3 , you should use iteritems()
>>> ("key1", 1), ("key2", 2)

so you can iterate on it :

for key, value in my_dict.items():
   do_the_stuff(key, value)
like image 119
Cédric Julien Avatar answered Dec 07 '22 22:12

Cédric Julien