By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,748 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,748 IT Pros & Developers. It's quick & easy.

dictionary with lists as values

rhitam30111985
100+
P: 112
ok.. here is the problem: consider the following dictionary..
it has a country name as a key and list of offices as values:

Expand|Select|Wrap|Line Numbers
  1. offices={'USA':['google','microsoft','apple','infosys'],
  2.        'ireland':['apple','accenture','dupont'],
  3.        'india':['infosys','accenture','TCS','dupont']
  4. }
  5.  
now supposed i enter the name of an office as an input form the user.. for example infosys... then i should get india and USA as my output...
how do i go about it?

thanks in advance
Aug 22 '07 #1
Share this Question
Share on Google+
4 Replies

bartonc
Expert 5K+
P: 6,596
ok.. here is the problem: consider the following dictionary..
it has a country name as a key and list of offices as values:

Expand|Select|Wrap|Line Numbers
  1. offices={'USA':['google','microsoft','apple','infosys'],
  2.        'ireland':['apple','accenture','dupont'],
  3.        'india':['infosys','accenture','TCS','dupont']
  4. }
  5.  
now supposed i enter the name of an office as an input form the user.. for example infosys... then i should get india and USA as my output...
how do i go about it?

thanks in advance
Expand|Select|Wrap|Line Numbers
  1. >>> for country, officeList in offices.items():
  2. ...     if "infosys" in officeList:
  3. ...         print country
  4. ...         
  5. india
  6. USA
  7. >>> 
Aug 22 '07 #2

rhitam30111985
100+
P: 112
Expand|Select|Wrap|Line Numbers
  1. >>> for country, officeList in offices.items():
  2. ...     if "infosys" in officeList:
  3. ...         print country
  4. ...         
  5. india
  6. USA
  7. >>> 
oh... looks like i gotta go over the manual more thoroughy... thanks a lot barton
Aug 22 '07 #3

bartonc
Expert 5K+
P: 6,596
oh... looks like i gotta go over the manual more thoroughy... thanks a lot barton
Any time, really. Have fun!

Note that the order of the output is indeterminate.
Aug 22 '07 #4

rhitam30111985
100+
P: 112
Any time, really. Have fun!

Note that the order of the output is indeterminate.
hmm.. order is indeterminate.. didnt notice that.. anyway.. that is not a problem...
Aug 22 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.