Thread: how to add?

    #1
  1. No Profile Picture
    404 Error
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    http://hester.dns2go.com
    Posts
    18
    Rep Power
    0

    how to add?


    i create a dictionary (i think thats the name):

    a = {"nr1":"Number 1"}

    but how do i add new keys?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Adding keys to a dictionary Python is very easy , it's allot like assigning a vaule to a variable i.e. dictionary[key] = value would add a key to the dictionary with a value

    Here's a small example of this from Python's shell..

    >>> dictionary = {'one': 1}
    >>> dictionary
    {'one': 1}
    >>> dictionary['two'] = 2
    >>> dictionary
    {'two': 2, 'one': 1}

    Note: dictionaries are unordered and don't need to be for obvious reasons

    Have fun,
    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    404 Error
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    http://hester.dns2go.com
    Posts
    18
    Rep Power
    0
    thanks!
  6. #4
  7. No Profile Picture
    Hi, I'm Calvin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    LosAngeles, SanDiego, Houston
    Posts
    50
    Rep Power
    11
    oh wow... that's a better than the way that i've been doing it.

    i've been doing this:

    dictionary.update({'two':2})

    well, ya learn something new everyday...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    Also, if you want to create a dictionary with string keys, you can write:
    Code:
    >>> dict(alfa="a", beta="b", gamma="g")
    {'beta': 'b', 'alfa': 'a', 'gamma': 'g'}

IMN logo majestic logo threadwatch logo seochat tools logo