Using tuples to eliminate multiple dict values

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Sep 16 21:28:07 CEST 2011


-----Original Message-----
From: python-list-bounces+ramit.prasad=jpmorgan.com at python.org [mailto:python-list-bounces+ramit.prasad=jpmorgan.com at python.org] On Behalf Of Benshep
Sent: Friday, September 16, 2011 1:48 PM
To: python-list at python.org
Subject: Using tuples to eliminate multiple dict values

I need a dictionary that returns the same value for multiple keys.

i.e.

(1)   >>> dict = { (1,2,3) : 'text' , (5,6,7) : 'other text' }
(2)   >>>dict[1]
(3)   'text'

I cant figure out what i need on line 2 to make this scenario work. Is
there a simple way to check if the a number is present in the key and
then return the value?

Thanks
----------------------------------------------------------------

I think I must be missing something. Can you not do the following?

>>> d = { 1:'text', 2:'text', 3:'text', 5:'other text', 6:'other text', 7:'other text' }
>>> d[1]
'text'



If you are they need to share the same value you can also do something like this.

>>> t1 = [ 'text' ]
>>> t2 = [ 'other text' ]
>>> d = { 1:t1, 2:t1, 3:t1, 5:t2, 6:t2, 7:t2 }
>>> d[1]
['text']
>>> d[1][0]='new text'
>>> d[2][0]
'new text'
>>> d
{1: ['new text'], 2: ['new text'], 3: ['new text'], 5: ['other text'], 6: ['other text'], 7: ['other text']}


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list