Thanks Arnaud<br><br>List comprehension method really works nicely.sorry for late reply.<br><br><br><span class="HOEnZb"><font color="#888888"><br><br><br></font></span><div class="aju"><br></div><span class="gD"></span><div class="gmail_quote">
On Mon, May 7, 2012 at 7:10 PM, Arnaud Delobelle <span dir="ltr"><<a href="mailto:arnodel@gmail.com" target="_blank">arnodel@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 7 May 2012 12:31, Nikhil Verma <<a href="mailto:varma.nikhil22@gmail.com">varma.nikhil22@gmail.com</a>> wrote:<br>
> HI All<br>
><br>
> I was clearing my concepts on dictionary and stuck in this problem.<br>
> I have a dictionary which i have formed by using zip function on two list so<br>
> that one list (which i have hardcoded) becomes the keys and the other list<br>
> becomes its values.<br>
><br>
> Now i want to know how can i get the values of keys at once if i pass the<br>
> keys in a dictionary.<br>
><br>
> Let say I have a dictionary<br>
><br>
> mydict = {'a':'apple' , 'b':'boy' ,'c' : 'cat', 'd':'duck','e':'egg'}<br>
><br>
> Now if i do :-<br>
><br>
> mydict.get('a')<br>
> 'apple'<br>
<br>
</div>mydict['a'] is the usual way to get the value associated with a key.<br>
The difference is that it will throw an exception if the key doesn't<br>
exist, which is most of the time the sanest thing to do.<br>
<div class="im"><br>
> What i want is some i pass keys in get and in return i should have all the<br>
> values of those keys which i pass.<br>
><br>
> ##################<br>
> mydict.get('a','b','c') ###demo for what i want<br>
> 'apple','boy','cat' ### Output i want<br>
> #################<br>
<br>
</div>1. You can use a list comprehension<br>
<br>
>>> [mydict[k] for k in 'a', 'b', 'c']<br>
['apple', 'boy', 'cat']<br>
<br>
2. You can use map (for python 3.X, you need to wrap this in list(...))<br>
<br>
>>> map(mydict.__getitem__, ['a', 'b', 'c'])<br>
['apple', 'boy', 'cat']<br>
<br>
3. You can use operator.itemgetter<br>
<br>
>>> from operator import itemgetter<br>
>>> itemgetter('a', 'b', 'c')(mydict)<br>
('apple', 'boy', 'cat')<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Arnaud<br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>Regards<br>Nikhil Verma<br>+91-958-273-3156<br><br>