I am working with python 3.1.1 Below is my code and this works well. -
dict={'paul':{'age':23,'hobby':'computer','like':'music'}}
-
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
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) - myDict['anotheritem']=dict() #creates empty dictionary
-
myDict['anotheritem']['nested']=True #populated new dictionary
Regarding the printing: - for d1 in myDict:
-
for d2 in myDict[d1]:
-
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...
Thanks. Its working. But to print, its not working.so i've used the below code to print -
mydict=dict()
-
mydict['a']=dict()
-
mydict['a']['nested']=True
-
for key1,value1 in mydict.items():
-
tempdict = dict(mydict[key1].items())
-
for key,value in tempdict.items():
-
print (key1,key,value)
And This is working good....
Thanks once again
bvdet 2,851
Recognized Expert Moderator Specialist
bharathpaul,
Please use code tags when posting code. See posting guidelines here.
BV - Moderator
Glenton 391
Recognized Expert Contributor
Oh, sorry. This is what you get when you don't run the code. It should be: - for d1 in myDict:
-
for d2 in myDict[d1]:
-
print d1, d2, myDict[d1][d2]
-
But you got the idea, and solved it yourself, which is great!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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__") ;
|
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...
|
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)
| |
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...
|
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...
|
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
|
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?
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |