364,111 Members | 2030 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How do i append data to a list contained inside a dict ?

Jory R Ferrell
P: 44
I need to assign lists containing class objects to a key (so i can create var names for a user defined numb of objts). I tried appending to the key of a list and couldnt get it to work. How would i manipulate objects when i rely on a dictionary to refer to them? :/
Jan 22 '12 #1

✓ answered by bvdet

You can easily modify dictionary values by reassignment or manipulation of the value or its attributes.
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {"a": 123, "b": 456}
  2. >>> dd["a"] += 321
  3. >>> dd
  4. {'a': 444, 'b': 456}
  5. >>> dd = {"a": [1,2,3], "b": [4,5,6]}
  6. >>> dd["a"].append(4)
  7. >>> dd
  8. {'a': [1, 2, 3, 4], 'b': [4, 5, 6]}
  9. >>> 
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,509
You can easily modify dictionary values by reassignment or manipulation of the value or its attributes.
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {"a": 123, "b": 456}
  2. >>> dd["a"] += 321
  3. >>> dd
  4. {'a': 444, 'b': 456}
  5. >>> dd = {"a": [1,2,3], "b": [4,5,6]}
  6. >>> dd["a"].append(4)
  7. >>> dd
  8. {'a': [1, 2, 3, 4], 'b': [4, 5, 6]}
  9. >>> 
Jan 23 '12 #2

Jory R Ferrell
P: 44
Hold one....maybe I wrote the code up wrong.... :P
Jan 23 '12 #3

Jory R Ferrell
P: 44
Yeh...I wrote it up wrong somehow yesterday....
ty
Jan 23 '12 #4

Post your reply

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python