Append data to a list within a dict

T

Tina I

Hello group,

Say I have the following dictionary:

ListDict = {
'one' : ['oneone' , 'onetwo' , 'onethree'],
'two' : ['twoone' , 'twotwo', 'twothree'],
'three' : ['threeone' , 'threetwo', threethree']}

Now I want to append 'twofour' to the list of the 'two' key but I can't
figure out how to that?
Some pointers would be greatly appreciated.

Thanks
Tina
 
M

Michael Bentley

Say I have the following dictionary:

ListDict = {
'one' : ['oneone' , 'onetwo' , 'onethree'],
'two' : ['twoone' , 'twotwo', 'twothree'],
'three' : ['threeone' , 'threetwo', threethree']}

Now I want to append 'twofour' to the list of the 'two' key but I
can't
figure out how to that?
Some pointers would be greatly appreciated.

ListDict['two'].append('twofour')

But you'll have to insert the missing single quote before
"threethree" first.

hope this helps,
Michael
 
P

Paul Rubin

Tina I said:
ListDict = {
'one' : ['oneone' , 'onetwo' , 'onethree'],
'two' : ['twoone' , 'twotwo', 'twothree'],
'three' : ['threeone' , 'threetwo', threethree']}

Now I want to append 'twofour' to the list of the 'two' key but I
can't figure out how to that?

Is this a class exercise? Hint:
1) figure out how to access the list of the 'two' key
2) append 'twofour' to it.
 
7

7stud

Hello group,

Say I have the following dictionary:

ListDict = {
'one' : ['oneone' , 'onetwo' , 'onethree'],
'two' : ['twoone' , 'twotwo', 'twothree'],
'three' : ['threeone' , 'threetwo', threethree']}

Now I want to append 'twofour' to the list of the 'two' key but I can't
figure out how to that?
Some pointers would be greatly appreciated.

Thanks
Tina


ListDict["two"] returns the list. So you can do this:

lst = listDict["two"]
lst.append("twofour")

or you can do it directly like this:

ListDict["two"].append("twofour")
 
T

Tina I

Paul said:
Tina I said:
ListDict = {
'one' : ['oneone' , 'onetwo' , 'onethree'],
'two' : ['twoone' , 'twotwo', 'twothree'],
'three' : ['threeone' , 'threetwo', threethree']}

Now I want to append 'twofour' to the list of the 'two' key but I
can't figure out how to that?

Is this a class exercise? Hint:
1) figure out how to access the list of the 'two' key
2) append 'twofour' to it.
He he... at the age of 40 I'm well beyond school work ;)
But thanks anyway

Tina
 
T

Tina I

Michael said:
Say I have the following dictionary:

ListDict = {
'one' : ['oneone' , 'onetwo' , 'onethree'],
'two' : ['twoone' , 'twotwo', 'twothree'],
'three' : ['threeone' , 'threetwo', threethree']}

Now I want to append 'twofour' to the list of the 'two' key but I can't
figure out how to that?
Some pointers would be greatly appreciated.

ListDict['two'].append('twofour')

But you'll have to insert the missing single quote before "threethree"
first.

hope this helps,
Michael
Great! Thanks!
And the missing singlequote was just a typo, my actual dictionary is way
bigger so I just made up this as an example.

Tina
 
H

Hendrik van Rooyen

Hello group,

Say I have the following dictionary:

ListDict = {
'one' : ['oneone' , 'onetwo' , 'onethree'],
'two' : ['twoone' , 'twotwo', 'twothree'],
'three' : ['threeone' , 'threetwo', threethree']}

Now I want to append 'twofour' to the list of the 'two' key but I can't
figure out how to that?
Some pointers would be greatly appreciated.

Just do it:

ListDict['two'].append('twofour')

This works because ListDict['two'] is a list...

- Hendrik
 
A

Alex Martelli

Tina I said:
He he... at the age of 40 I'm well beyond school work ;)

Why would that be? My wife's over 40, yet she's a student (currently at
Stanford -- they were overjoyed to admit her, with lot of life
experience as well as previous studies, apparently). She's not taking
elementary courses on Python (having co-written a book on Python,
tech-reviewed a few, and having been the first woman coopted as a member
of the PSF:), but she _has_ been taken equivalent ones on Java and C++
(languages she didn't previously know), as well as calculus,
neurophysiology, and other strange things that are part of the "Symbolic
Systems" studies.

Down with ageism!-)


Alex
 
T

Tina I

Alex said:
Why would that be? My wife's over 40, yet she's a student (currently at
Stanford -- they were overjoyed to admit her, with lot of life
experience as well as previous studies, apparently). She's not taking
elementary courses on Python (having co-written a book on Python,
tech-reviewed a few, and having been the first woman coopted as a member
of the PSF:), but she _has_ been taken equivalent ones on Java and C++
(languages she didn't previously know), as well as calculus,
neurophysiology, and other strange things that are part of the "Symbolic
Systems" studies.

Down with ageism!-)


Alex
It's not really ageism, just how I feel *my self* about going back to
school. I'm not done learning though, it's just that I can give my self
the luxury of learning whatever I want, just what I want at the pace
that I want. And since I happen to like computers that's mostly what I
concentrate about. Right now I'm hooked on Python, but also messing
about with PHP and Javascript. Tried some Java but didn't like it...
Threw out Windows a couple of years ago and have spent quite some time
learning the ins and outs of Linux.

So even if I probably will never go back to school I'm looking forward
to many many years learning new and exciting things :)

Tina
(Sorry for the way OT post. I just couldn't stop my own ramble ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top