Two variable dictionary comprehension

Rob Gaddi rgaddi at highlandtechnology.invalid
Mon Apr 3 13:38:15 EDT 2017


On 04/03/2017 10:27 AM, Deborah Swanson wrote:
> Dennis Lee Bieber wrote, on April 03, 2017 9:35 AM
>>
>> On Mon, 3 Apr 2017 07:30:40 -0700, "Deborah Swanson"
>> <python at deborahswanson.net> declaimed the following:
>>
>>>
>>> Clearly there's more to be found in nooks, crannies and
>> byways in the
>>> docs than you'll get to from the given pointers in the
>> index. Maybe it
>>> would be worthwhile to scrape the whole mess and have it in
>> searchable
>>> text form. Another thing Python would be the right tool for the job
>>> for. Regular updates as the docs are updated would be a good
>> idea too.
>>> It's obvious that today's Google isn't up to it, although it
>> occurs to
>>> me that I haven't tried Google's site search on python.org.
>>>
>> 	On Windows, the (at least, for ActiveState releases)
>> documentation is available in Windows Help format -- though
>> I'll admit the "free text search" leaves a lot to be desired...
>>
>> 	"dict comprehension" didn't find anything obvious;
>> "dictionary comprehension" brought up PEP 274 (note: I still
>> use 2.7 as main version).
>>
>> -=-=-=-=-=-
>> Semantics
>>     The semantics of dict comprehensions can actually be demonstrated
>>     in stock Python 2.2, by passing a list comprehension to the
>>     builtin dictionary constructor:
>>
>>     >>> dict([(i, chr(65+i)) for i in range(4)])
>>
>>     is semantically equivalent to
>>
>>     >>> {i : chr(65+i) for i in range(4)}
>>
>>     The dictionary constructor approach has two dictinct disadvantages
>>     from the proposed syntax though.  First, it isn't as legible as a
>>     dict comprehension.  Second, it forces the programmer to create an
>>     in-core list object first, which could be expensive.
>>
>> -=-=-=-=-=-
>> --
>> 	Wulfraed                 Dennis Lee Bieber         AF6VN
>>     wlfraed at ix.netcom.com    HTTP://wlfraed.home.netcom.com/
>
> It would be interesting to play around with different list
> comprehensions than the one they've shown.
>
> I'll admit that both dictionaries and comprehensions are still a little
> bit fuzzy to me, especially when I get away from the common usages. This
> could be a good exercise to clarify some of the fuzzy areas.
>
> Deborah
>

And don't forget
   dict_comp : { k:v for (k,v) in kv_provider }
   set_comp : { item for item in item_provider }
   set_of_tuples_comp : { (k,v) for (k,v) in kv_provider }

Just to make things more complicated.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list