[Tutor] Python Idioms?

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Wed Apr 1 12:04:34 CEST 2015


On 04/01/2015 11:04 AM, Alan Gauld wrote:
> On 01/04/15 05:50, Jim Mooney wrote:
>
>>>>> s = [1,2,3,4,5,6,7,8]
>>>>> list(zip(*[iter(s)]*2))
>>>>> [(1, 2), (3, 4), (5, 6), (7, 8)]
>
> Personally I'd have used slicing in this example:
>
> zip(s[::2],s[1::2])
>

With an emphasis on *in this example*.

The idiom you are citing works on any iterable, not all of which support 
slicing and the slicing version requires two passes over the data.



More information about the Tutor mailing list