list_a-list_b

Philip Swartzleonard starx at pacbell.net
Wed Apr 17 06:21:46 EDT 2002


Tim Peters || Tue 16 Apr 2002 10:54:56p:

> [David Eppstein]
>> ...
>> Better is to convert b to a dictionary
>> (where are dict comprehensions when you need them):
>>
>>     dict_b = {}
>>     for x in list_b: dict_b[x] = 1
> 
> Looks clear enough to me, although
> 
>     for x in list_b:
>         dict_b[x] = 1
> 
> is clearer.  If you like, you can do this:
> 
>     dict([(x, 1) for x in list_b])
> 
> or this:
> 
>     dict(zip(list_b, [1]*len(list_b)))
> 
> or even:
> 
>     dict(zip(list_b, list_b))
> 
> since you don't really care what the dict's values are, just the keys.
>  I draw the line before:
> 
>     dict(zip(*[list_b]*2))
> 
> though.  Contrary to popular belief, there's no Fabulous Prize
> awaiting those who minimize either vertical or horizontal space
> <wink>. 

Minimizing relative vertical space is great; that's why list 
comprehensions are better than nested for loops for expressing simple 
ideas ('i want var to be an x by y matrix of foozles' =), and why moving 
stuff into subfunctions is nice. Horizontal space though... feh. Unless 
you're using emacs default python mode, in a two or three level deep 
looping structre in a function in a class, and you need to write 
'self.data[x][y].variable = self.long_function_name( lots, of, arguments 
)' ... =)




-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list