I want to convert a list like this
l1 = [1,2,3,4,5,6,7,8]
to
l2 = [(1,2),(3,4),(5,6),(7,8)]
because want to loop
for x,y in l2:
draw_thing(x,y)
I want to convert a list like this
l1 = [1,2,3,4,5,6,7,8]
to
l2 = [(1,2),(3,4),(5,6),(7,8)]
because want to loop
for x,y in l2:
draw_thing(x,y)
One good way is:
from itertools import izip
it = iter([1, 2, 3, 4])
for x, y in izip(it, it):
print x, y
Output:
1 2
3 4
>>>
Building on Nick D's answer:
>>> from itertools import izip
>>> t = [1,2,3,4,5,6,7,8,9,10,11,12]
>>> for a, b in izip(*[iter(t)]*2):
... print a, b
...
1 2
3 4
5 6
7 8
9 10
11 12
>>> for a, b, c in izip(*[iter(t)]*3):
... print a, b, c
...
1 2 3
4 5 6
7 8 9
10 11 12
>>> for a, b, c, d in izip(*[iter(t)]*4):
... print a, b, c, d
...
1 2 3 4
5 6 7 8
9 10 11 12
>>> for a, b, c, d, e, f in izip(*[iter(t)]*6):
... print a, b, c, d, e, f
...
1 2 3 4 5 6
7 8 9 10 11 12
>>>
Not quite as readable, but it shows a compact way to get any size tuple you want.
Take a look at grouper function from itertools docs.
from itertools import izip_longest
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
In your case use it like this:
l1 = [1,2,3,4,5,6,7,8]
for (x, y) in grouper(2, l1):
draw_thing(x, y)
You can do:
l2 = []
for y in range(0, len(l1), 2):
l2.append((l1[y], l1[y+1]))
I'm not doing any checks to make sure l1 has an even number of entries and such-like.
Not the most elegant solution
l2 = [(l1[i], l1[i+1]) for i in xrange(0,len(l1),2)]
No need to construct a new list. You can just iterate over the list by steps of 2 instead of 1. I use len(L) - 1
as the upper-bound so you ensure that you don't try to access past the end of the list.
for i in range(0, len(L) - 1, 2):
draw_thing(L[i], L[i + 1])
list = [1,2,3,4,5,6]
it = iter(list)
newlist = [(x, y) for x, y in zip(it, it)]
What's wrong with just accessing the correct index and incrementing?
for (int i=0;i<myList.Length;i++)
{
draw_thing(myList[i],myList[++i]);
}
Oops - sorry, in C# mode. I'm sure you get the idea.