pylint woes
DFS
nospam at dfs.com
Tue May 10 18:36:34 EDT 2016
On 5/7/2016 10:50 PM, Chris Angelico wrote:
> On Sun, May 8, 2016 at 12:15 PM, DFS <nospam at dfs.com> wrote:
>> The only reason
>>
>> for j in range(len(list1)):
>> do something with list1[j], list2[j], list3[j], etc.
>>
>> or
>>
>> for item1, item2, item3 in zip(list1, list2, list3):
>> do something with the items
>>
>> works is because each list has the same number of items.
>
> Sure, but who cares what each item's position is? All that matters is
> that they have corresponding positions, which is what zip() does.
They have corresponding positions because zip() possibly truncates data!
> Imagine you don't even have the whole lists yet. Imagine someone's
> still writing stuff to them as you work. Maybe they're infinite in
> length. You can't iterate up to the length of list1, because it
> doesn't HAVE a length. But you can still zip it up with other parallel
> collections, and iterate over them all.
Disregarding a list of infinite length.
If lists are still being created:
* at every moment in time, len(list1) returns a length that doesn't
change even if data is added to the list after the call to len().
Example: If the list has 100 items in it at the point len(list) is called:
for i in range(len(list1))
will never iterate more than 100x, no matter how large list1 grows to.
Caveat: since list1 may be bigger or smaller than the other lists at
that moment in time, an error may occur when using list2[i], list3[i].
Is that all correct as you understand it?
* at every moment in time, zip(list1, list2, etc) will return a fixed,
same-length lists of tuples, which doesn't change even if data is added
to any of the lists after the call to zip().
Example: if the lists have 100, 97 and 102 items in them at the point
zip(lists) is called:
for item1, item2, item3 in zip(list1, list2, list3)
will never iterate beyond 97x, even if the lists grow while the
enumeration is occurring.
Caveat: since zip() possibly truncates lists, the results - the usage of
the data - could be completely invalid.
Is that all correct as you understand it?
So, if that's all correct, it doesn't matter whether you use 'for i in
range(len(lists))' or 'for item in zip(lists)': neither will guarantee
data integrity. I haven't timed them, but as I see it, neither has a
definite advantage over the other.
So what I decided to do was build the lists, check that the lengths are
all the same (exit the program if not), zip() them, and use enumeration
because it looks less a little less clunky. I see no advantage beyond
appearance.
More information about the Python-list
mailing list