Discussion:
Is there any nice way to unpack a list of unknown size??
Fredrik Lundh
2008-09-14 19:39:29 UTC
Permalink
1. first, second, third, *rest = foo
update to Python 3.0 (as others have pointed out), or just do

first, second, third = foo[:3]
rest = foo[3:]

for item in list_of_lists:
a, b, c = item[:3]
rest = item[3:]
...

and move on to more interesting parts of your program.

</F>
Tim Chase
2008-09-14 19:16:04 UTC
Permalink
1. first, second, third, *rest = foo
Python 3.0 has exactly this feature. No current Python 2.x version has it.
I asked something similar[1] on c.l.p a while back and Diez
Roggisch gave this nice workaround/hack[2]

It's a bit ugly in the implementation (sniffing the stack), but
elegant in the use, doing exactly what I needed.

It doesn't accommodate the "rest" portion that you reference, but
allows for arbitrary unpacking into a fixed-length tuple.

-tkc


[1]
http://mail.python.org/pipermail/python-list/2006-May/381386.html

[2]
http://mail.python.org/pipermail/python-list/2006-May/381399.html
Gary Herron
2008-09-14 15:08:08 UTC
Permalink
1. first, second, third, *rest = foo
Python 3.0 has exactly this feature. No current Python 2.x version has it.

Gary Herron
Please suggest.
Thanks,
Srini
Bring your gang together. Do your thing. Find your favourite Yahoo! group at http://in.promos.yahoo.com/groups/
--
http://mail.python.org/mailman/listinfo/python-list
Grant Edwards
2008-09-15 00:20:10 UTC
Permalink
Post by Fredrik Lundh
1. first, second, third, *rest = foo
update to Python 3.0 (as others have pointed out), or just do
first, second, third = foo[:3]
rest = foo[3:]
Of course you can do that in one line if you want it to look a
bit more like the original pseudocode:

(a,b,c),rest = foo[:3],foo[3:]

That still requires you to manually count the number of
"non-rest" destination elements on the LHS and type that number
twice on the RHS. If you wanted to elminate a tiny bit of the
redundancy you could define a split() function:

def split(seq,n):
return seq[:n],seq[n:]

(a,b,c),rest = split(foo,3)
--
Grant
srinivasan srinivas
2008-09-14 14:57:20 UTC
Permalink
I want to do something like below:

1. first, second, third, *rest = foo

?2. for (a,b,c,*rest) in list_of_lists:

Please suggest.

Thanks,
Srini


Bring your gang together. Do your thing. Find your favourite Yahoo! group at http://in.promos.yahoo.com/groups/
Arnaud Delobelle
2008-09-14 17:45:58 UTC
Permalink
1. first, second, third, *rest = foo
Python 3.0 has exactly this feature. ?No current Python 2.x version has it.
Gary Herron
Please suggest.
Thanks,
Srini
? ? ? Bring your gang together. Do your thing. Find your favourite Yahoo! group athttp://in.promos.yahoo.com/groups/
--
http://mail.python.org/mailman/listinfo/python-list
In python >= 2.4, you can define a function like this:

def truncate(iterable, n=1):
iterator = iter(iterable)
for i in iterator:
if n == 0:
yield iterator
return
yield i
n -= 1
a, b, c, tail = truncate([1,2,3,4,5,6], 3)
a
1
b
2
c
3
tail
<listiterator object at 0x78990>
list(tail)
[5, 6]

--
Arnaud

Continue reading on narkive:
Loading...