[Tutor] how to convert array into tuple
kent37 at tds.net
Thu Sep 27 21:05:50 CEST 2007
Noufal Ibrahim wrote:
> This seems to work although I'd like comments from the more experienced
> Pythonistas out there.
> >>> foo
> [1, 2, 3, 4, 5, 6, 7, 8, 9]
> >>> [(foo[i],foo[i+1]) for i in range(0,len(foo)-2,2)]
> [(1, 2), (3, 4), (5, 6), (7, 8)]
This problem is a perennial favorite on comp.lang.python and in the
cookbook, which I take to mean that there is no clear best solution. You
can see some alternatives here
and in the links at the bottom of that page.
One question is what to do with partial groups at the end; your version
truncates the original list.
More information about the Tutor