473,549 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ 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 1699
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
3575
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 respectively In the sample code you see that I have class Item and class Dict class Dict contains a dictionary called items. The items dictionary...
4
2507
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 want to # init a class from that dictionary. # However, I don't want to specify the # dictionary gets by hand # since it is error prone.
0
1022
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 = PyImport_AddModule("__main__") ;
0
2037
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 into a sql insert query so i don't have to enter all that data manually. I would like some recommendation on the best way i could get this data into...
1
9242
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 DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
7
3781
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, Delegate); using the standard .Add method of the Dictionary, passing in the name of the delegate instance for the value. private void...
1
2403
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 the meaning in a text area (as any encyclopedia) and the term in a textfield. My questions: 1. How can i load the chemistry terms in to the JList...
2
3063
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 section: Discography --------------------- DiscID
6
3883
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 suggestion?
1
2269
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) Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>(); Now I am using GetDictionaryType Function...
0
7532
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7461
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7491
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1068
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
776
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.