Walking lists

L

lallous

Hello


I am still learning Python, and have a question, perhaps I can shorten
the code:

L = (
(1, 2, 3),
(4,),
(5,),
(6, 7)
)

for x in L:
print x

What I want, is to write the for loop, something like this:

for (first_element, the_rest) in L:
print first_element
for x in the_rest:
# now access the rest of the elements

I know I can :
for x in L:
first = x[0]
rest = x[1:]
....
Probably that is not possible, but just asking.

Thanks,
Elias
 
P

Peter Otten

lallous said:
I am still learning Python, and have a question, perhaps I can shorten
the code:

L = (
(1, 2, 3),
(4,),
(5,),
(6, 7)
)

for x in L:
print x

What I want, is to write the for loop, something like this:

for (first_element, the_rest) in L:
print first_element
for x in the_rest:
# now access the rest of the elements

I know I can :
for x in L:
first = x[0]
rest = x[1:]
....
Probably that is not possible, but just asking.

In Python 3 you can write
.... print("first:", first, "rest:", rest)
....
first: 1 rest: [2, 3]
first: 4 rest: []
first: 5 rest: []
first: 6 rest: [7]

Peter
 
T

Tim Chase

lallous said:
L = (
(1, 2, 3),
(4,),
(5,),
(6, 7)
)

What I want, is to write the for loop, something like this:

for (first_element, the_rest) in L:
print first_element
for x in the_rest:
# now access the rest of the elements

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)
I know I can :
for x in L:
first = x[0]
rest = x[1:]

However in 2.x, this is the way to do it. Though if you want to
abstract the logic, you can move it to a generator:

def splitter(i):
for t in i:
yield t[0], t[1:]

for first, rest in splitter(L):
print first
for x in rest:
do_stuff(x)

-tkc



[*] not having py3 on this machine, I can't readily verify this.
 
A

Arnaud Delobelle

lallous said:
Hello


I am still learning Python, and have a question, perhaps I can shorten
the code:

L = (
(1, 2, 3),
(4,),
(5,),
(6, 7)
)

for x in L:
print x

What I want, is to write the for loop, something like this:

for (first_element, the_rest) in L:
print first_element
for x in the_rest:
# now access the rest of the elements

I know I can :
for x in L:
first = x[0]
rest = x[1:]

Others have replied about Python 3. In Python 2.x, you can use an
iterator:

for tuple in L:
it = iter(tuple)
first = it.next()
for x in it:
...

HTH
 
L

lallous

Thank you all for the replies.

The solution using Python 3's syntax look very intuitive.

Thanks Tim, Arnaud for the idea (I am using 2.x)
 
J

Jean-Michel Pichavant

lallous said:
Thank you all for the replies.

The solution using Python 3's syntax look very intuitive.

Thanks Tim, Arnaud for the idea (I am using 2.x)

--
Elias
Hello

I am still learning Python, and have a question, perhaps I can shorten
the code:

L = (
(1, 2, 3),
(4,),
(5,),
(6, 7)
)

for x in L:
print x

What I want, is to write the for loop, something like this:

for (first_element, the_rest) in L:
print first_element
for x in the_rest:
# now access the rest of the elements

I know I can :
for x in L:
first = x[0]
rest = x[1:]
....
Probably that is not possible, but just asking.

Thanks,
Elias
Using slicing + list comprehension with python 2.x

for first, rest in [(e[0],e[1:]) for e in L]:
print first
print rest


1
(2, 3)
4
()
5
()
6
(7,)


But honestly, the code you provided is just fine.

JM
 
M

Mensanator

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.
(4,),
(5,),
(6,7)
) print('first',first,end=' ')
print('rest',rest)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top