[Tutor] Python Idioms?

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Apr 1 22:59:30 CEST 2015


On 01/04/2015 18:20, Roel Schroeven wrote:
> Mark Lawrence schreef:
>> On 01/04/2015 11:50, Alan Gauld wrote:
>>> On 01/04/15 11:04, Wolfgang Maier wrote:
>>>> 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.
>>> Agreed, but readability always trumps performance,
>>> unless performance is critical.
>>> In which case readability usually trumps performance.
>>>
>>> And especially on a beginners list.
>>>
>>
>> In which case I'll stick with the more-itertools pairwise() function
>
> Which does not the same thing. Original:
>
>  >>>>>>>> s = [1,2,3,4,5,6,7,8]
>  >>>>>>>> list(zip(*[iter(s)]*2))
>  >>>>>>>> [(1, 2), (3, 4), (5, 6), (7, 8)]
>
> Pairwise:
>
>>  >>> take(4, pairwise(count()))
>> [(0, 1), (1, 2), (2, 3), (3, 4)]
>
>
> Greetings,
> Roel
>

Yo are of course completely correct, I was conflating two different 
threads :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list