zip list, variables

Peter Otten __peter__ at web.de
Thu Nov 21 03:58:54 EST 2013


flebber wrote:

> Thank you for the replies.
> 
> Looking at the replies I am wondering which solution is more scalable. At
> the moment it is only 2 nested lists but what about 5, 10, 20 or more?
> 
> Should I start looking into numpy to handle this or will list
> comprehension
>   >>> [ [ x + y for x, y in zip(x,y) ] for x, y in zip(a,b) ]
> Be sufficient ?

I would certainly prefer

>>> a + b
array([[ 6,  8],
       [10, 12]])

over the incomprehensible comprehension. But if it is the only usecase for 
numpy in your script and you are OK with its current performance, just put 
your listcomp into an aptly named function. Then you can write the easily 
understandable

c = matrix_add(a, b)

and avoid the numpy dependency. 




More information about the Python-list mailing list