By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,385 Members | 1,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,385 IT Pros & Developers. It's quick & easy.

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

Jory R Ferrell
P: 62
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 23 '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,851
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: 62
Hold one....maybe I wrote the code up wrong.... :P
Jan 23 '12 #3

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

Post your reply

Sign in to post your reply or Sign up for a free account.