473,486 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inserting in multidimension dictionary.

6 New Member
I am working with python 3.1.1 Below is my code and this works well.
Expand|Select|Wrap|Line Numbers
  1. dict={'paul':{'age':23,'hobby':'computer','like':'music'}}
  2. print (dict['paul']['hobby'])
But how to insert a new item in the above dictionary.
If i tried with, say
dict['anotheritem']['nested']='true' , it gives error...
Also help me in printing the elements using Loop
Mar 9 '10 #1
4 1690
Glenton
391 Recognized Expert Contributor
Hi

There may be a better way to do this, but in general I've always added the outer dictionary and then populated it.

(BTW better not to use dict, since it's taken! I've changed it to myDict below)

Expand|Select|Wrap|Line Numbers
  1. myDict['anotheritem']=dict() #creates empty dictionary
  2. myDict['anotheritem']['nested']=True #populated new dictionary
Regarding the printing:
Expand|Select|Wrap|Line Numbers
  1. for d1 in myDict:
  2.     for d2 in myDict[d1]:
  3.         print d1, d2, myDict[d2]
should work (ie I haven't tried it out)

Good luck, and please post back to tell us how it goes...
Mar 9 '10 #2
bharathpaul
6 New Member
Thanks. Its working. But to print, its not working.so i've used the below code to print
Expand|Select|Wrap|Line Numbers
  1. mydict=dict()
  2. mydict['a']=dict()
  3. mydict['a']['nested']=True
  4. for key1,value1 in mydict.items():
  5.     tempdict = dict(mydict[key1].items())
  6.     for key,value in tempdict.items():
  7.        print (key1,key,value)
And This is working good....
Thanks once again
Mar 9 '10 #3
bvdet
2,851 Recognized Expert Moderator Specialist
bharathpaul,

Please use code tags when posting code. See posting guidelines here.

BV - Moderator
Mar 9 '10 #4
Glenton
391 Recognized Expert Contributor
Oh, sorry. This is what you get when you don't run the code. It should be:
Expand|Select|Wrap|Line Numbers
  1. for d1 in myDict:
  2.     for d2 in myDict[d1]:
  3.         print d1, d2, myDict[d1][d2]
  4.  
But you got the idea, and solved it yourself, which is great!
Mar 10 '10 #5

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

Similar topics

1
3572
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
4
2500
by: brianobush | last post by:
# # My problem is that I want to create a # class, but the variables aren't known # all at once. So, I use a dictionary to # store the values in temporarily. # Then when I have a complete set, I...
0
1012
by: quadric | last post by:
Hi, I have an application that has an embedded interpreter. This application loads many DLL's and passes a PyObject * to each DLL that was gotten from the following call: PyObject * pmod =...
0
2029
by: GreenFROG Design | last post by:
Data Processing and SQL INSERTING Hello guy's i'm faced with a horrible dilemma i have whole dictionary that i have to enter into a database. I have all the text and i would like to convert it...
1
9237
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
7
3771
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
1
2397
by: Salha | last post by:
Hello there, Can you help me in following problem: Am working on a chemistry dictionary. I want to have all the chemistry terms in a JList and whenever i select a term from a JList, it displays...
2
3055
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
6
3878
by: gita ziabari | last post by:
Hello All, The following code does not work for unicode characters: keyword = dict() kw = 'ÇÅÎÓËÉÈ' keyword.setdefault(key, ).append (kw) It works fine for inserting ASCII character. Any...
1
2263
by: sachin2 | last post by:
I am using 3 types of dictionaries. 1) Dictionary<string, string > d = new Dictionary<string, string>(); 2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); 3)...
0
7126
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
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
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
7330
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...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.