Walking lists

Mensanator mensanator at aol.com
Thu Feb 25 13:59:39 EST 2010


On Feb 25, 7:02 am, Tim Chase <python.l... at tim.thechases.com> wrote:
> Python 3 introduced a variable tuple assignment which I
> suspect[*] would work in this context:
>
>    for first, *rest in L: # note the asterisk
>      print first
>      for x in rest:
>        do_stuff(x)
>
> [*] not having py3 on this machine, I can't readily verify this.

Seems fine under 3.1. Cool.

>>> L = ((1,2,3),
     (4,),
     (5,),
     (6,7)
     )
>>> for first, *rest in L:
	print('first',first,end=' ')
	print('rest',rest)

first 1 rest [2, 3]
first 4 rest []
first 5 rest []
first 6 rest [7]



More information about the Python-list mailing list