473,395 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to append to a dictionary

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

May 19 '06 #1
3 43801
> groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}

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

How do I append this to 'groups'?


groups['ITALIAN'] = 'orange'

as described at

http://docs.python.org/tut/node7.htm...00000000000000

-tkc

May 19 '06 #2
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
May 19 '06 #3
"Harlin Seritt" <ha**********@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

May 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Elena Schulz | last post by:
Hi, I've the following code: myList = for i in range(3) : myDict=i myList.append(myDict) myList becomes: but I want:
16
by: Ling Lee | last post by:
Hello. I'm trying to write a small program that lets you put in a number as an integer and then it tells you the textuel representation of the number. Like if your input is 42, it will say...
57
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
3
by: Roland Rickborn | last post by:
Hi folks, I am relatively new to Python. Although I read a lot of howtos, introductions and wikis, I am still having trouble ;-) My querstion: As the subject says, I'd like to feed a...
7
by: Brian Elmegaard | last post by:
Hi I have written the following which works, but I would like to write it less clumsy. I have a dictionary in which I loop through the keys for a dynamic programming algorithm. If a key is...
10
by: Ben | last post by:
Hello... I have set up a dictionary into whose values I am putting a list. I loop around and around filling my list each time with new values, then dumping this list into the dictionary. Or so I...
6
by: Tina I | last post by:
Hi everyone, I have a small, probably trivial even, problem. I have the following HTML: I need to make this into a dictionary like this: dictionary = {"METAR:" : "ENBR 270920Z 00000KT 9999...
4
by: =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= | last post by:
Hi Pythonistas! I've got a question about storing tuples in a dictionary. First, a small test case which creates a list of dictionaries: import time list_of_dicts = keys = prev_clk =...
0
by: Gary Herron | last post by:
Dan Upton wrote: Yes. Create a list of keys, and loop through it: pids = procs_dict.keys() for pid in pids: if procs_dict.poll() != None # do the counter updates del procs_dict Then the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.