Reputation: 13
cities={
'city1':{
'name':'sydney',
'country':'australia',
'desc':'beautiful'
},
'city2':{
'name':'toronto',
'country':'canada',
'desc':'amazing',
}
}
cities['city3']:"{'name':'Tokyo','country':'japan','desc':'lots of earthquakes’}"
for keys,values in cities.items():
print(f"{keys}--->{values}”)
This is my code. I am new to python and learning dictionaries as of now. I am trying to add a dictionary to an existing dictionary but it doesn’t work. I have no errors and still only get the first two cities info. I think my syntax must be wrong. Can anyone help me with this please>?
Output:
Upvotes: 1
Views: 69
Reputation: 11
Use Chainmap from collections, it chains all dictionary into one.
from collections import ChainMap
city3 = {'city3': {'name':'Tokyo','country':'japan','desc':'lots of earthquakes'}}
cities = ChainMap(cities,city3)
print(dict(cities))
Upvotes: 0
Reputation: 156
Try to change your insertion code to:
cities['city3'] = {'name':'Tokyo','country':'japan','desc':'lots of earthquakes'}
You probably don't want to add it as a string, so leave away the quatation marks. Furthermore, there is an erroneous quatation mark at the end of the description.
Upvotes: 2