xxxxxxxxxx
#traversing dictionary function
d={1:'one',2:'two',3:'three',4:'four'}
print(d)
for i in d:
print(i,d[i])
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
# Loop through dictionary in key order
di = {'b': 2, 'c': 3, 'a': 1}
for k, v in sorted(di.items()):
print(k, v)
# a 1
# b 2
# c 3
xxxxxxxxxx
students = {
'John': 85,
'Emily': 92,
'Michael': 78,
'Sarah': 89
}
for student in students:
print(student, students[student])