954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Manipulating dictionaries

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!

Devlan
Newbie Poster
13 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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:

>>> d = {'a':1, 'b':2, 'c':3}
>>> d['d'] = 4
>>> d['a'] = [ d['a'], 12, 13 ]
>>> d['e'] = []
>>> d['e'].append('foobar')
>>> d
{'a': [1, 12, 13], 'c': 3, 'b': 2, 'e': ['foobar'], 'd': 4}
>>>
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You