How to append to a dictionary

Ben Finney bignose+hates-spam at benfinney.id.au
Fri May 19 20:46:05 EDT 2006


"Harlin Seritt" <harlinseritt at yahoo.com> writes:

> groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}
> 
> I want to add another key: 'ITALIAN' : 'orange'
> 
> How do I append this to 'groups'?

Dictionary items have no implicit sequence, so you don't "append" to
one.

Assigning any value to a key in the dictionary will create that key if
it doesn't exist.

    >>> groups = {'IRISH': 'green', 'AMERICAN': 'blue'}
    >>> groups['ITALIAN'] = 'orange'
    >>> print groups
    {'IRISH': 'green', 'AMERICAN': 'blue', 'ITALIAN': 'orange'}


Please enjoy your trip through the Python tutorial, working through
the examples and understanding each one.

    <URL:http://docs.python.org/tut/>

-- 
 \        "It is the responsibility of intellectuals to tell the truth |
  `\                                and expose lies."  -- Noam Chomsky |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list