Manipulating dictionaries

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Manipulating dictionaries

 
0
  #1
Oct 20th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Manipulating dictionaries

 
0
  #2
Oct 20th, 2008
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:
  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. >>>
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 460 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC