Kindly show me a better way to do it
Alain Ketterlin
alain at dpt-info.u-strasbg.fr
Sat May 8 16:56:25 EDT 2010
Oltmans <rolf.oltmans at gmail.com> writes:
> a = [ [1,2,3,4], [5,6,7,8] ]
>
> Currently, I'm iterating through it like
>
> for i in [k for k in a]:
> for a in i:
> print a
I would prefer:
for i in a:
for v in i:
print v
i.e., not messing with a and avoiding an additional list.
> but I was wondering if there is a shorter, more elegant way to do it?
I can't see any, but if you need to save space, you may use
for v in (e for l in a for e in l):
...
-- Alain.
More information about the Python-list
mailing list