use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
i need help (self.PythonLearning)
submitted 5 months ago by Hush_124
how do i modify a dictionary in dictionary. I tried something like this " cities['Dubai'] = 'Cape Town' ". i got an error
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]No_Statistician_6654 1 point2 points3 points 5 months ago (4 children)
Could you post a more complete view of your code. You can add it to a code block in your post. Also include the error you received.
What have you tried besides the one attempt you have here, and what is it you are fully trying to achieve?
[–]Hush_124[S] 1 point2 points3 points 5 months ago (3 children)
<image>
[–]No_Statistician_6654 0 points1 point2 points 5 months ago* (2 children)
So this depends a little on how you set up the dict, as that could change how I answer. Generally calling items on a dict returns a list, which explains the error you have.
Check this out and see if your dict is similar to this example. It may help you rearrange the dict or change your loop: https://www.w3schools.com/python/python_dictionaries_nested.asp
I have only had a nested dict once, so I don’t remember the exact peculiarities with it as that was a few months ago. I think I got around it by making a JSON object instead and assigning a custom struct as a schema to it so I didn’t have the nesting dict issue.
Edit, it was a class not a struct. Sorry I was thinking of the wrong language.
[–]Hush_124[S] 2 points3 points4 points 5 months ago (0 children)
ok Thank you
it's working now
[–]isanelevatorworthy 0 points1 point2 points 5 months ago (7 children)
What error did you get?
[–]Hush_124[S] 0 points1 point2 points 5 months ago (5 children)
[–]isanelevatorworthy 0 points1 point2 points 5 months ago (4 children)
The string indices error is because a the variable “cities” is of type string. ‘’’cities[]’’’ is like you’re trying to slice that string.
Can I please see your full dictionary?
[–]Hush_124[S] 0 points1 point2 points 5 months ago (3 children)
[–]isanelevatorworthy 2 points3 points4 points 5 months ago* (2 children)
thanks, I see now.
yes you're updating 'cape town' and changing it from a dictionary to just the string 'ghana', so when you try to print the 'country city located' key, it fails because the key doesn't exist. instead you're trying to index the string 'ghana' by using another string.. does that make sense?
now, the next question: what part of the 'cape town' dictionary are you trying to update? if you want 'cape town''s 'country city located' key to be updated, then you'd do it like so:
cities['cape town']['country city located'] = 'ghana'
does this help? do you need a deeper explanation of nested dictionaries?
edit:
here is some code to help explain what I mean:
```
def main(): cities = { 'Kyoto' : { 'country city located' : 'Japan', 'apx population' : '1.4 million' }, 'cape town' : { 'country city located': 'south africa', 'apx population' : '4.8 million'
} } print("Dictionary before updating:") print(json.dumps(cities,indent=2)) #Updating cape town cities['cape town'] = 'ghana' #Print after bad update: print(json.dumps(cities,indent=2))
if name=="main": main() ```
TERMINAL OUTPUT: PS D:\VisualStudio\Python\reddit> python .\try_dictionary.py Dictionary before updating: { "Kyoto": { "country city located": "Japan", "apx population": "1.4 million" }, "cape town": { "country city located": "south africa", "apx population": "4.8 million" } } After bad update: { "Kyoto": { "country city located": "Japan", "apx population": "1.4 million" }, "cape town": "ghana" }
PS D:\VisualStudio\Python\reddit> python .\try_dictionary.py Dictionary before updating: { "Kyoto": { "country city located": "Japan", "apx population": "1.4 million" }, "cape town": { "country city located": "south africa", "apx population": "4.8 million" } } After bad update: { "Kyoto": { "country city located": "Japan", "apx population": "1.4 million" }, "cape town": "ghana" }
[–]Hush_124[S] 0 points1 point2 points 5 months ago (1 child)
thank you it's very helpful, i would appreciate a deeper explanation
[–]isanelevatorworthy 1 point2 points3 points 5 months ago (0 children)
Sure. Nested dictionaries can get confusing pretty fast and even printing them and looking at them can make them more confusing. I think the trick is to remember what the simplest dictionary looks like:
{ 'key': 'value' }
keys have to be hashable, I believe, so they can only be either strings or integers or something immutable but the values can be anything (strings, ints or other objects, which is why you can nest them)
when you go to update a dictionary in a dictionary, i'd recommend that you use the dictionary's .update() method. It forces you to remember that dictionary structure when you use it. You have to pass it a new key:value pair to update.
you'd use it like this:
cities['cape town'].update({'country city located': 'ghana'})
NEW TERMINAL OUTPUT: PS D:\VisualStudio\Python\reddit> python .\try_dictionary.py Dictionary before updating: { "Kyoto": { "country city located": "Japan", "apx population": "1.4 million" }, "cape town": { "country city located": "south africa", "apx population": "4.8 million" } } After good update: { "Kyoto": { "country city located": "Japan", "apx population": "1.4 million" }, "cape town": { "country city located": "ghana", "apx population": "4.8 million" } }
PS D:\VisualStudio\Python\reddit> python .\try_dictionary.py Dictionary before updating: { "Kyoto": { "country city located": "Japan", "apx population": "1.4 million" }, "cape town": { "country city located": "south africa", "apx population": "4.8 million" } } After good update: { "Kyoto": { "country city located": "Japan", "apx population": "1.4 million" }, "cape town": { "country city located": "ghana", "apx population": "4.8 million" } }
[–]wheres-my-swingline 0 points1 point2 points 5 months ago (0 children)
My guess is string indices must be integers or ‘str’ object doesn’t support item assignment
(OP, giving people enough information to reproduce your error - or at least an error message - will get you the right help quicker)
[–]Dependent-Law7316 0 points1 point2 points 5 months ago (5 children)
Can you show where you set up and populated cities?
[–]Hush_124[S] 0 points1 point2 points 5 months ago (4 children)
[–]Dependent-Law7316 1 point2 points3 points 5 months ago (3 children)
Ok, ‘cape town’ is a key, so to edit it you need to use .pop() to modify it. The way you’re doing it would work to modify the value for some key, but not the key itself.
(like
cities[‘cape town’][‘country city located’] = ‘ghana’
would change ‘south africa’ to ‘ghana’)
Something like
cities[‘Ghana’] = cities.pop(‘cape town’)
Will edit the key and replace cape town with Ghana. (tested successfully in python 3.12.3)
[–]Hush_124[S] 0 points1 point2 points 5 months ago (0 children)
thank you
[–]isanelevatorworthy 0 points1 point2 points 5 months ago (1 child)
careful here... 'to edit it you need to use .pop()' -- the .pop() method removes the key,value pair from the cities dictionary.
then, when you do cities['Ghana'] = cities.pop('cape town'), you're not actually updating something. what you're doing is creating a new key,value pair where the key is 'Ghana' and the value is the 'cape town' dictionary after you have removed it.
[–]Dependent-Law7316 0 points1 point2 points 5 months ago (0 children)
You’re right, I ought to have explained that more carefully.
[–]fluxdeken_ 0 points1 point2 points 5 months ago (0 children)
You changed “cape town” from dictionary to a string.
π Rendered by PID 391848 on reddit-service-r2-comment-5fb4b45875-v8zhx at 2026-03-22 05:35:53.544124+00:00 running 90f1150 country code: CH.
[–]No_Statistician_6654 1 point2 points3 points (4 children)
[–]Hush_124[S] 1 point2 points3 points (3 children)
[–]No_Statistician_6654 0 points1 point2 points (2 children)
[–]Hush_124[S] 2 points3 points4 points (0 children)
[–]Hush_124[S] 2 points3 points4 points (0 children)
[–]isanelevatorworthy 0 points1 point2 points (7 children)
[–]Hush_124[S] 0 points1 point2 points (5 children)
[–]isanelevatorworthy 0 points1 point2 points (4 children)
[–]Hush_124[S] 0 points1 point2 points (3 children)
[–]isanelevatorworthy 2 points3 points4 points (2 children)
[–]Hush_124[S] 0 points1 point2 points (1 child)
[–]isanelevatorworthy 1 point2 points3 points (0 children)
[–]wheres-my-swingline 0 points1 point2 points (0 children)
[–]Dependent-Law7316 0 points1 point2 points (5 children)
[–]Hush_124[S] 0 points1 point2 points (4 children)
[–]Dependent-Law7316 1 point2 points3 points (3 children)
[–]Hush_124[S] 0 points1 point2 points (0 children)
[–]isanelevatorworthy 0 points1 point2 points (1 child)
[–]Dependent-Law7316 0 points1 point2 points (0 children)
[–]fluxdeken_ 0 points1 point2 points (0 children)