xxxxxxxxxx
dictionary = {52:"E",126:"A",134:"B",188:"C",189:"D"}
for key, value in dictionary.items():
print(key)
print(value)
xxxxxxxxxx
a_dict = {'apple':'red', 'grass':'green', 'sky':'blue'}
for key in a_dict:
print key # for the keys
print a_dict[key] # for the values
xxxxxxxxxx
d = {'x': 1, 'y': 2, 'z': 3}
for key in d:
print key, 'corresponds to', d[key]
xxxxxxxxxx
# define a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
# loop through the dictionary
for key, value in my_dict.items():
# print the key and value
print(key, value) # a 1, b 2, c 3
# loop through the keys of the dictionary
for key in my_dict.keys():
# print the key
print(key) # a, b, c
# loop through the values of the dictionary
for value in my_dict.values():
# print the value
print(value) # 1, 2, 3
xxxxxxxxxx
my_dic = {
'name': 'Majhi',
'age': 71,
'country': 'BD'
}
for key, value in my_dic.items():
print(key) # name age country
print(value) # Majhi 71 BD
xxxxxxxxxx
students = {
'John': 85,
'Emily': 92,
'Michael': 78,
'Sarah': 89
}
for student in students:
print(student, students[student])
xxxxxxxxxx
jjj = {'chuck': 1, 'fred': 42, 'jan': 100}
# If you want only the keys
for key in jjj:
print(key)
# if you want only the values
for key in jjj:
print(jjj[key])
# if you want both keys and values with items
# Using the above you can get either key or value separately if you want
for key, value in jjj.items():
print(key, value)
xxxxxxxxxx
python = {
"year released": 2001,
"creater":"Guido Van Rossum"
}
for x in python.values():
print(x)