473,396 Members | 1,917 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,396 software developers and data experts.

dictionary with lists as values

rhitam30111985
112 100+
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
4 1186
bartonc
6,596 Expert 4TB
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
112 100+
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
6,596 Expert 4TB
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
112 100+
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

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

Similar topics

15
by: Andy C | last post by:
I am new to python, so please bear with me if I am making some conceptual error. Basically I want to create a graph with an adjacency list representation, but I don't want any of the adjacency...
2
by: neutrinman | last post by:
Hi,there. How can I choose a key in dictionary randomly? Say, random.choice() in lists, or in lists: lists = position = random.range(len(lists)) word = lists
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:
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
7
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
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...
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 =...
10
by: ++imanshu | last post by:
Hi, Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. Thanks,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.