Making a dict from two lists/tuples

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu May 24 11:24:57 EDT 2001


Alex Martelli wrote:
>> Definitely worth using, provided you have first profiled the code and
>> know that it is a bottleneck. 
> 
> As per my previous post in response to Emile, I don't think
> the normal "first profile &c" approach is quite suitable to
> a technique that qualifies as "an idiom".  I don't wait to
> profile &c before I code a case of "build up a big string
> from many little pieces" in terms of a list (with .append,
> or a comprehension, or...) and a final ''.join rather than
> the most straightforward "loop-on-string +=" -- I've learned
> once and for all that the latter is just too slow for real
> use, so I automatically use the "fancy" idiom instead.  As
> I use it often, gradually I stop perceiving it as "fancy";
> it becomes, for me, THE "obviously correct" way to approach
> a certain frequent category of tasks in Python...

I agree about needing to build up a set of idioms you are happy using. I 
think though that in the case of string += vs build a list and join it, the 
differences can be quite substantial whereas in the converting list pairs 
to a dictionary the conclusion seems to be to avoid using zip. The other 
options are sufficiently close to one another that using the clearest one 
for the situation should outweigh any speed gains.

Does anyone know if there is a collection of preferred Python idioms 
anywhere?


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list