This is the dictionary
JavaScript
x
5
1
cars = {'A':{'speed':70,
2
'color':2},
3
'B':{'speed':60,
4
'color':3}}
5
Using this for loop
JavaScript
1
4
1
for keys,values in cars.items():
2
print(keys)
3
print(values)
4
It prints the following:
JavaScript
1
5
1
B
2
{'color': 3, 'speed': 60}
3
A
4
{'color': 2, 'speed': 70}
5
But I want the program to print it like this:
JavaScript
1
7
1
B
2
color : 3
3
speed : 60
4
A
5
color : 2
6
speed : 70
7
I just started learning dictionaries so I’m not sure how to do this.
Advertisement
Answer
JavaScript
1
5
1
for x in cars:
2
print (x)
3
for y in cars[x]:
4
print (y,':',cars[x][y])
5
output:
JavaScript
1
7
1
A
2
color : 2
3
speed : 70
4
B
5
color : 3
6
speed : 60
7