How to append to a dictionary

H

Harlin Seritt

I have some code here:

groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}

I want to add another key: 'ITALIAN' : 'orange'

How do I append this to 'groups'?

Thanks,

Harlin Seritt
 
S

Sybren Stuvel

Harlin Seritt enlightened us with:
I have some code here:

groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}

I want to add another key: 'ITALIAN' : 'orange'

How do I append this to 'groups'?

groups['ITALIAN'] = 'orange'

Sybren
 
B

Ben Finney

Harlin Seritt said:
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/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,539
Members
48,274
Latest member
HowardKipp

Latest Threads

Top