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
  need to compare 2 values in a nested dictionary jss 2 855 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Change the key value in dictionary quest 3 1,427 Mar-25-2022, 07:41 AM
Last Post: ndc85430
  Nested dictionary acting strange Pedroski55 2 2,085 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 4,530 May-03-2021, 04:45 PM
Last Post: nostradamus64
Lightbulb Python Nested Dictionary michaelserra 2 2,594 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  nested dictionary rkpython 7 3,257 May-29-2020, 11:13 AM
Last Post: rkpython
  Nested Dictionary/List tonybrown3 5 3,149 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help: for loop with dictionary and nested lists mart79 1 1,864 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Transforming nested key-tuples into their dictionary values ClassicalSoul 4 2,658 Apr-11-2020, 04:36 PM
Last Post: bowlofred
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,367 Jul-02-2019, 02:05 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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