|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
how to add?
i create a dictionary (i think thats the name):
a = {"nr1":"Number 1"} but how do i add new keys? |
|
#2
|
||||
|
||||
|
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 valueHere'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. |
|
#3
|
|||
|
|||
|
thanks!
|
|
#4
|
|||
|
|||
|
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... |
|
#5
|
|||
|
|||
|
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'}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > how to add? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|