On Tue, May 27, 2014 at 2:45 PM, Michael Urman <mur...@gmail.com> wrote:
> On Tue, May 27, 2014 at 4:05 AM, Chris Angelico <ros...@gmail.com> wrote:
>> On Tue, May 27, 2014 at 6:58 PM, Serhiy Storchaka <storch...@gmail.com> 
>> wrote:
>>> 26.05.14 10:59, raymond.hettinger написав(ла):
>>>>
>>>> +        result = [(elem, i) for i, elem in zip(range(n), it)]
>>>
>>>
>>> Perhaps it is worth to add simple comment explaining why this is not
>>> equivalent to just list(zip(it, range(n))). Otherwise it can be
>>> unintentionally "optimized" in future.
>>>
>>
>> Where is the difference? I'm very much puzzled now. My first thought
>> was based on differing-length iterables in zip, but the docs say it
>> stops at the shortest of its args.
>
> Due to how zip stops, it leaves the longer iterable in different places:
>
>>>> it = iter(string.ascii_letters); list(zip(range(3), it)); next(it)
> [(0, 'a'), (1, 'b'), (2, 'c')]
> 'd'
>>>> it = iter(string.ascii_letters); list(zip(it, range(3))); next(it)
> [('a', 0), ('b', 1), ('c', 2)]
> 'e'
>
> This seems like a potentially nasty gotcha, but I'm unclear what real
> use cases would be impacted.

If that's the issue, a comment explaining it should definitely be added.

We could also use islice(enumerate(it), n)) to avoid the confusion.

- Tal
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to