Traverse a dictionary with for loop Accessing keys and values in dictionary. Use Dict.values() and Dict.keys() to generate keys and values as iterable. Nested Dictionaries with for loop Access Nested values of Nested Dictionaries How useful was this post? Click on a star to rate it! Submit Rating
dict={1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',10:'ten'}dictO/P:{1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',10:'ten'}for i indict:print(dict[i])O/P:onetwothreefourfivesixseveneightnineten
Accessing keys and values in dictionary.
for i indict:print(i,dict[i])O/P:1 one2 two3 three4 four5 five6 six7 seven8 eight9 nine10 ten
Use Dict.values() and Dict.keys() to generate keys and values as iterable.
for key indict.keys():print(key)O/P:12345678910for value indict.values():print(value)O/P:onetwothreefourfivesixseveneightnineten
Nested Dictionaries with for loop
Access Nested values of Nested Dictionaries
dict={'Apple':{'color':'red','taste':'sweet'},'Lemon':{'color':'yellow','taste':'sour'},'Cherry':{'color':'red','taste':'sweet'},}for i indict:print(i)for j in dict[i]:print(j,dict[i][j])print()O/P:Applecolor redtaste sweetLemoncolor yellowtaste sourCherrycolor redtaste sweet