473,574 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dictionary with lists as values

rhitam30111985
112 New Member
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 1191
bartonc
6,596 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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
2620
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 lists to have duplicate strings when it is avoidable. I have a function createEdge that adds an edge to the graph. The arguments will be distinct...
2
13723
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
7133
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
10753
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 future Python versions and let dict.keys() and dict.values() both return sets instead of lists. If d is a dict, code like: for x in d.keys():
7
20966
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 way of expressing this, but I have a problem with the way lists are represented when converted to strings. Lets say my dictionary is
11
6060
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 dicts with larger no. of features. Now i want to check each pair to see if they are connected...element of this pair will be one from the first...
14
3447
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 list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the...
10
2324
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 thought... It would appear that what I am dumping into the dictionary value is only a pointer to the original list, so after all my iterations...
4
9799
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 = time.clock()
10
1972
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, ++imanshu
0
7826
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7741
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8085
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8126
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5637
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5328
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.