473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Updating values in nested dictionary

1
I'm looking for a way to update a nested dict dict1
dic1 = {'key1': {'key2': {'key3': {'key4': {'key51': {'key511': 'val511', 'key512': 'val512'}}, 'key52': {'key521': {'key5211': 'val5211', 'key5212': 'val5212'}}}}}}

I want to replace the element in dic1: {'key51': {'key511': 'val511', 'key512': 'val512'}} to 'val4'

So I can get a new dictionary as the following:
dic2 = {'key1': {'key2': {'key3': {'key4': 'val4', 'key52': {'key521': {'key5211': 'val5211', 'key5212': 'val5212'}}}}}}

I can do this manually using the following statement

>>> dic1['key1']['key2']['key3']['key4'] = 'val4'
Expand|Select|Wrap|Line Numbers
  1. >>> dic1
  2. {'key1': {'key2': {'key3': {'key52': {'key521': {'key5211': 'val5211', 'key5212': 'val5212'}}, 'key4': 'val4'}}}}
but I got stuck in writing Python code for this problem

Your help would be very much appreciated. Thank you.
Oct 19 '11 #1
2 4878
bvdet
2,851 Expert Mod 2GB
The way you updated the dictionary manually would be the way to do it. What about writing code for it is a problem? Are you trying to copy the dictionary and change a value?
Oct 19 '11 #2
dwblas
626 Expert 512MB
When a dictionary is nested more than one or two levels deep, you should consider using SQLite instead http://www.wdvl.com/Authoring/python...s07162009.html
Oct 23 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: omission9 | last post by:
I have a dictionary that looks like this MY_DICT=FOO I am having a problem updating this with a simple MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting into the inner...
2
by: kbass | last post by:
I am new to Python and I am attempting to retrieve data from a database and I would like to place this data into a nested dictionary. After placing the data into a dictionary, I would like to loop...
1
by: Sean Berry | last post by:
If I have a dictionary like the following: {'category' : 2, 'shape', 4} How can I use this to make category = 2 and shape = 4. I want to be able to do this regardless of the dict values...
0
by: Knepley, Jim | last post by:
I'm basing some work on Joe Celko's excellent idea of using nested sets to represent an organizational structure as opposed to an adjacency list. By and large it's a great idea, but not without its...
3
by: Jake Emerson | last post by:
I'm attempting to build a process that helps me to evaluate the performance of weather stations. The script below operates on an MS Access database, brings back some data, and then loops through to...
8
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
1
by: Sam Loxton | last post by:
Hi, I am fairly new to the python language and am trying to sort a nested Dictionary of a Dictionary which I wish to sort by value. The dictionary does not have to be restructured as I only need...
0
by: DaTurk | last post by:
Hi, I have a generic dictionary defined, and when I attpet to return a reference to it via a property I get this error error C3073: 'System::Collections::Generic::Dictionary<TKey,TValue>' : ref...
3
by: nkyaelly | last post by:
Hi People, I am getting frustrated with the way Python handles its dictionary. I have a scenarion where a class A is stored in a dictionary. The class A contains a dictionary B as a field as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.