earksiinni
0
Q:

loop throughthe key and the values of a dict in python

dictionary = {52:"E",126:"A",134:"B",188:"C",189:"D"}
for key, value in dictionary.items():
	print(key)
	print(value)
8
new_list = [something(key, value) for key, value in a_dict.items()]
14
d = {'x': 1, 'y': 2, 'z': 3} 
for key in d:
    print key, 'corresponds to', d[key]
8
# Python3 code to iterate through all key, value  
# pairs in a dictionary 
  
statesAndCapitals = { 
                     'Gujarat' : 'Gandhinagar', 
                     'Maharashtra' : 'Mumbai', 
                     'Rajasthan' : 'Jaipur', 
                     'Bihar' : 'Patna'
                    } 
  
# Iterating over values 
for state, capital in statesAndCapitals.items(): 
    print(state, ":", capital) 
2
a_dict = {"color": "blue", "fruit": "apple", "pet": "dog"}

# Will loop through the dict's elements (key, value) WITHOUT ORDER
for key, value in a_dict.items():
  print(key, '->', value)
0

New to Communities?

Join the community