Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

return respective values when mutiple keys are passed in dictionary

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


varma.nikhil22 at gmail

May 7, 2012, 4:31 AM

Post #1 of 5 (379 views)
Permalink
return respective values when mutiple keys are passed in dictionary

HI All

I was clearing my concepts on dictionary and stuck in this problem.
I have a dictionary which i have formed by using zip function on two list
so that one list (which i have hardcoded) becomes the keys and the other
list
becomes its values.

Now i want to know how can i get the values of keys at once if i pass the
keys in a dictionary.

Let say I have a dictionary

mydict = {'a':'apple' , 'b':'boy' ,'c' : 'cat', 'd':'duck','e':'egg'}

Now if i do :-

mydict.get('a')
'apple'

What i want is some i pass keys in get and in return i should have all the
values of those keys which i pass.

##################
mydict.get('a','b','c') ###demo for what i want
'apple','boy','cat' ### Output i want
#################

--
Regards
Nikhil Verma
+91-958-273-3156


hash.3g at gmail

May 7, 2012, 4:36 AM

Post #2 of 5 (372 views)
Permalink
Re: return respective values when mutiple keys are passed in dictionary [In reply to]

You can try to use map(mydict.get, ('a', 'b', 'c')) and then make join
On May 7, 2012 2:33 PM, "Nikhil Verma" <varma.nikhil22 [at] gmail> wrote:

> HI All
>
> I was clearing my concepts on dictionary and stuck in this problem.
> I have a dictionary which i have formed by using zip function on two list
> so that one list (which i have hardcoded) becomes the keys and the other
> list
> becomes its values.
>
> Now i want to know how can i get the values of keys at once if i pass the
> keys in a dictionary.
>
> Let say I have a dictionary
>
> mydict = {'a':'apple' , 'b':'boy' ,'c' : 'cat', 'd':'duck','e':'egg'}
>
> Now if i do :-
>
> mydict.get('a')
> 'apple'
>
> What i want is some i pass keys in get and in return i should have all the
> values of those keys which i pass.
>
> ##################
> mydict.get('a','b','c') ###demo for what i want
> 'apple','boy','cat' ### Output i want
> #################
>
> --
> Regards
> Nikhil Verma
> +91-958-273-3156
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


arnodel at gmail

May 7, 2012, 6:40 AM

Post #3 of 5 (372 views)
Permalink
Re: return respective values when mutiple keys are passed in dictionary [In reply to]

On 7 May 2012 12:31, Nikhil Verma <varma.nikhil22 [at] gmail> wrote:
> HI All
>
> I was clearing my concepts on dictionary and stuck in this problem.
> I have a dictionary which i have formed by using zip function on two list so
> that one list (which i have hardcoded) becomes the keys and the other list
> becomes its values.
>
> Now i want to know how can i get the values of keys at once if i pass the
> keys in a dictionary.
>
> Let say I have a dictionary
>
> mydict = {'a':'apple' , 'b':'boy' ,'c' : 'cat', 'd':'duck','e':'egg'}
>
> Now if i do :-
>
> mydict.get('a')
> 'apple'

mydict['a'] is the usual way to get the value associated with a key.
The difference is that it will throw an exception if the key doesn't
exist, which is most of the time the sanest thing to do.

> What i want is some i pass keys in get and in return i should have all the
> values of those keys which i pass.
>
> ##################
> mydict.get('a','b','c')    ###demo for what i want
> 'apple','boy','cat'        ### Output i want
> #################

1. You can use a list comprehension

>>> [mydict[k] for k in 'a', 'b', 'c']
['apple', 'boy', 'cat']

2. You can use map (for python 3.X, you need to wrap this in list(...))

>>> map(mydict.__getitem__, ['a', 'b', 'c'])
['apple', 'boy', 'cat']

3. You can use operator.itemgetter

>>> from operator import itemgetter
>>> itemgetter('a', 'b', 'c')(mydict)
('apple', 'boy', 'cat')

--
Arnaud
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

May 7, 2012, 6:45 AM

Post #4 of 5 (369 views)
Permalink
Re: return respective values when mutiple keys are passed in dictionary [In reply to]

On Mon, May 7, 2012 at 9:31 PM, Nikhil Verma <varma.nikhil22 [at] gmail> wrote:
> mydict = {'a':'apple' , 'b':'boy' ,'c' : 'cat', 'd':'duck','e':'egg'}
>
> Now if i do :-
>
> mydict.get('a')
> 'apple'
>
> What i want is some i pass keys in get and in return i should have all the
> values of those keys which i pass.
>
> ##################
> mydict.get('a','b','c') ###demo for what i want
> 'apple','boy','cat' ### Output i want
> #################

Presumably you want to get back a list or tuple, so a list
comprehension is your best bet.

[mydict.get(i) for i in ('a','b','c')]

Incidentally, are you aware that dictionaries can be subscripted? The
get() method is good when you want a default value for anything that
doesn't exist, otherwise you can simply use:

mydict['a']

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


varma.nikhil22 at gmail

May 8, 2012, 11:56 PM

Post #5 of 5 (368 views)
Permalink
Re: return respective values when mutiple keys are passed in dictionary [In reply to]

Thanks Arnaud

List comprehension method really works nicely.sorry for late reply.






On Mon, May 7, 2012 at 7:10 PM, Arnaud Delobelle <arnodel [at] gmail> wrote:

> On 7 May 2012 12:31, Nikhil Verma <varma.nikhil22 [at] gmail> wrote:
> > HI All
> >
> > I was clearing my concepts on dictionary and stuck in this problem.
> > I have a dictionary which i have formed by using zip function on two
> list so
> > that one list (which i have hardcoded) becomes the keys and the other
> list
> > becomes its values.
> >
> > Now i want to know how can i get the values of keys at once if i pass the
> > keys in a dictionary.
> >
> > Let say I have a dictionary
> >
> > mydict = {'a':'apple' , 'b':'boy' ,'c' : 'cat', 'd':'duck','e':'egg'}
> >
> > Now if i do :-
> >
> > mydict.get('a')
> > 'apple'
>
> mydict['a'] is the usual way to get the value associated with a key.
> The difference is that it will throw an exception if the key doesn't
> exist, which is most of the time the sanest thing to do.
>
> > What i want is some i pass keys in get and in return i should have all
> the
> > values of those keys which i pass.
> >
> > ##################
> > mydict.get('a','b','c') ###demo for what i want
> > 'apple','boy','cat' ### Output i want
> > #################
>
> 1. You can use a list comprehension
>
> >>> [mydict[k] for k in 'a', 'b', 'c']
> ['apple', 'boy', 'cat']
>
> 2. You can use map (for python 3.X, you need to wrap this in list(...))
>
> >>> map(mydict.__getitem__, ['a', 'b', 'c'])
> ['apple', 'boy', 'cat']
>
> 3. You can use operator.itemgetter
>
> >>> from operator import itemgetter
> >>> itemgetter('a', 'b', 'c')(mydict)
> ('apple', 'boy', 'cat')
>
> --
> Arnaud
>



--
Regards
Nikhil Verma
+91-958-273-3156

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.