How to filter a dictionary ?

Nikhil Verma varma.nikhil22 at gmail.com
Tue Apr 10 03:31:25 EDT 2012


Thanks Dave and Shashank . I cleared the concept also.
I got it guys. In my piece of code where i was doing this

In [25]: [k for k,v in for_patient_type.iteritems() if v == "Real"]
Out[25]: [80, 81, 83, 84, 91, 93, 79]


thats what shashank suggest later. Thanks to you Dave.I cleared my concept
which i just forgot.

On Tue, Apr 10, 2012 at 12:54 PM, Shashank Singh <
shashank.sunny.singh at gmail.com> wrote:

>
>
> On Tue, Apr 10, 2012 at 12:16 AM, Dave Angel <d at davea.name> wrote:
>
>> On 04/10/2012 02:04 AM, Shashank Singh wrote:
>> > On Mon, Apr 9, 2012 at 10:49 PM, Nikhil Verma <varma.nikhil22 at gmail.com
>> >wrote:
>> > <SNIP>
>> >> I am trying this but its giving me a generator object.
>> >>
>> >> In [9]: (k for k,v in for_patient_type.iteritems() if v == 'Real')
>> >>
>> > Iterating over a dict gives you all the keys, not the key value pairs
>> >
>>
>> But that line does not iterate over the dict, it iterates over an
>> iterator consisting of key/value pairs.  Note he had a call to
>> iteritems().
>>
>
> Thanks Dave.
> My bad. Nikhil, you could get the data that you wanted by your initial
> approach. All you needed was to either run through the generator or just
> use list comprehension
>
> >>> g = (k for k,v in for_patient_type.iteritems() if v == 'Real')
> >>> for k in g: print k
> ...
> 80
> 81
> 83
> 84
> 91
> 93
> 79
> >>>
>
>
> >>> [k for k,v in for_patient_type.iteritems() if v == 'Real']
> [80, 81, 83, 84, 91, 93, 79]
>
>
> --
> Regards
> Shashank Singh
> http://www.flipora.com
> http://r <http://www.cse.iitb.ac.in/%7Eshashanksingh>
> ationalpie.wordpress.com
>
>


-- 
Regards
Nikhil Verma
+91-958-273-3156
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120410/1b81f2f2/attachment-0001.html>


More information about the Python-list mailing list