Reputation: 133
I need to use a for loop over a dictionary to display all the corresponding values
shops = {
'Starbucks': {
'type':'Shops & Restaurants',
'location':'Track 19'
},
'Supply-Store': {
'type':'Services',
'location':'Central Station'
}
}
for shop in shops:
print(shop + " is a: " +shop.items(0))
What I want my for loop to do is to take one item at a time, and then get the corresponding type and location. Right now, I'm stuck at getting the corresponding types and locations.
Expected Output would be:
Starbucks is a Shops & Restaurants located at Track 19.
Supply-Store is a Services located at Central Station.
Upvotes: 2
Views: 92
Reputation: 180401
You can use str.format passing the dict using **
to access arguments by name:
Shops = {
'Starbucks': {
'type':'Shops & Restaurants',
'location':'Track 19'
},
'Supply-Store': {
'type':'Services',
'location':'Central Station'
}
}
for shop,v in Shops.items():
print("{} is a {type} located at {location}".format(shop,**v))
Output:
Starbucks is a Shops & Restaurants located at Track 19
Supply-Store is a Services located at Central Station
Upvotes: 1
Reputation: 1228
Assuming each value in your shops
dictionary to be another dictionary with type and location.
What you want might be -
for key,value in shops.items():
print(key + " is a: " + value['type'] + " at : " + value['location'])
Upvotes: 3