List comprehension iterating over two lists is not working as expected
Your first list comprehension is equivalent to:
res = []
for ver in version:
for link in useragents:
if ver > 60:
res.append(link)
Notice you have nested loop with time complexity O(n2), i.e. you are iterating over every combination of version
and useragents
. That's not what you want, assuming your version
and useragents
lists are aligned.
The equivalent of your for
loop is the following list comprehension:
res = [link for link, ver in zip(useragents, version) if ver > 60]
This
[link for ver in version for link in useragents if ver > 60]
is not the same as zip. It's not iterating through the two sequences in parallel. It's iterating through all combinations of those two sequences.
It is as if you wrote:
for ver in version:
for link in useragents:
if ver > 60:
# append(link)
So if both sequences had length 5, there would be 25 combinations (some of which are filtered out by the condition ver > 60
).
When you want to go through sequences in parallel, zip
is the way to do it, even in a comprehension.
[link for (link, ver) in zip(useragents, version) if ver > 60]
Alternatively you can use the function compress()
in combination with map()
, where you check some condition:
from itertools import compress
filter_ = map(lambda x: x > 60, version)
list(compress(useragents, filter_))
Example:
s = 'ABCDEFG'
nums = range(len(s))
filter_ = map(lambda x: x > 3, nums)
print(list(compress(s, filter_)))
# ['E', 'F', 'G']
[link for (link, ver) in zip(useragents, version) if ver > 60]
You still have to zip the two lists together.