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 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) - 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...
|
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...
|
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 =...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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)...
|
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,...
| |
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...
|
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...
|
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: 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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
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...
| |