Back
This is the dictionary
cars = {'A':{'speed':70, 'color':2}, 'B':{'speed':60, 'color':3}}
cars = {'A':{'speed':70,
'color':2},
'B':{'speed':60,
'color':3}}
Using this for loop
for keys,values in cars.items(): print(keys) print(values)
for keys,values in cars.items():
print(keys)
print(values)
It prints the following:
B {'color': 3, 'speed': 60} A {'color': 2, 'speed': 70}
B
{'color': 3, 'speed': 60}
A
{'color': 2, 'speed': 70}
But I want the program to print it like this:
B color : 3 speed : 60 A color : 2 speed : 70
color : 3
speed : 60
color : 2
speed : 70
I just started learning dictionaries so I'm not sure how to do this.
To get the desired output that you want for yourself you can use the following code:-
for x in cars: print (x) for y in cars[x]: print (y,':',cars[x][y])
for x in cars:
print (x)
for y in cars[x]:
print (y,':',cars[x][y])
To know more about this you can have a look at the following video tutorial:-
31k questions
32.8k answers
501 comments
693 users