Get dictionary key

I just started with Python,

Is there any iteration in the dictionary like in PHP

foreach(aData as key=>value) 
+4
source share
3 answers

It looks 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:

 for key, value in my_dict.items(): do_the_stuff(key, value) 
+9
source

Assuming python 2:

 for key, value in aData.iteritems(): 
+5
source

Using:

 for key in dictionary.keys() value = dictionary[key] #do something 

OR:

 for key,value in dictionary.items(): #do something 
+4
source

All Articles