943,935 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 909
  • Python RSS
Oct 20th, 2008
0

Manipulating dictionaries

Expand Post »
Here I go with yet another of my don't-get-the-syntax-problems. For some reason I tend to get a lot more from putting a question here than reading through a few dozen pages of documentation, which I have once again done.

Say I have a dictionary as a basic data structure in a program. I'd like to be able - as a user input - to add additional keys to existing values and additonal values to existing keys. Everything I read feels like it comes almost to the point of explaining it and then says something like "And that's all of it".
Any hints would be great!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Oct 20th, 2008
0

Re: Manipulating dictionaries

You can't necessarily "add additional keys to existing values", unless you simply meant add additional key/value pairs to the existing dictionary. Here's some examples of adding elements to a dictionary:
python Syntax (Toggle Plain Text)
  1. >>> d = {'a':1, 'b':2, 'c':3}
  2. >>> d['d'] = 4
  3. >>> d['a'] = [ d['a'], 12, 13 ]
  4. >>> d['e'] = []
  5. >>> d['e'].append('foobar')
  6. >>> d
  7. {'a': [1, 12, 13], 'c': 3, 'b': 2, 'e': ['foobar'], 'd': 4}
  8. >>>
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Problem with MakeMenuBar()
Next Thread in Python Forum Timeline: Python Help - decimal to binary program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC