Python Forum
How to change value in a nested dictionary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change value in a nested dictionary?
#1
I have a dictionary whose keys are strings and the values are other, nested dictionaries.
The values ( nested dictionaries ) have different numbers of items. From 2 to 33.
How can I change a certain value of a certain nested dictionary that have 25 items without rewriting the nested dictionary manually by using for example:

nested_dict_1['user-1'] = 'sky'
the dictionary looks like this:
prime_dict = {'user-1': {'cars': 3, 'byckes': 8, 'bus-tickets': 27, 'shoes': 48}, 
              'user-2': {'appels': 2, 'babanas':7, 'limo':11, 'peaches': 9, 'mellons': 11, 'grapps': 31, 'potatos': 38}}
I want to change the value 11 to 15 for the key 'limo' in the nested dictionary that is a part of the value to the key 'user-2'.
How can I do this without to rewrite manually the hole value of the key 'user-2'?
Reply
#2
prime_dict['user-2']['limo'] = 15
A way to think about it: prime_dict['user-2'] gives you the sub-dictionary {'appels': 2, 'babanas':7, 'limo':11, 'peaches': 9, 'mellons': 11, 'grapps': 31, 'potatos': 38}, and then ['limo'] allows you to change the appropriate value.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-23-2019, 02:54 PM)ichabod801 Wrote:
prime_dict['user-2']['limo'] = 15
A way to think about it: prime_dict['user-2'] gives you the sub-dictionary {'appels': 2, 'babanas':7, 'limo':11, 'peaches': 9, 'mellons': 11, 'grapps': 31, 'potatos': 38}, and then ['limo'] allows you to change the appropriate value.

Super! Thanks a lot!
if the fans of jerry springer are more than the fans of carl sagan they will rule over us! what we gonna do?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
  need to compare 2 values in a nested dictionary jss 2 2,974 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Change the key value in dictionary quest 3 2,686 Mar-25-2022, 07:41 AM
Last Post: ndc85430
  Nested dictionary acting strange Pedroski55 2 3,645 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 8,528 May-03-2021, 04:45 PM
Last Post: nostradamus64
Lightbulb Python Nested Dictionary michaelserra 2 4,501 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  nested dictionary rkpython 7 5,681 May-29-2020, 11:13 AM
Last Post: rkpython
  Nested Dictionary/List tonybrown3 5 5,153 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help: for loop with dictionary and nested lists mart79 1 2,875 Apr-12-2020, 02:52 PM
Last Post: TomToad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020