[Tutor] Selecting from list

Steven D'Aprano steve at pearwood.info
Fri Jul 19 07:09:09 CEST 2013


On 19/07/13 10:18, Jim Mooney wrote:
> On 18 July 2013 10:27, Hs Hs <ilhs_hs at yahoo.com> wrote:
[...]
>> (sorry this is not homework question. I want to avoid looping, because I
>> have 300K lines to parse through)
>>
>> Thanks
>> Hs.
>
> Not sure what you want to do. If you only want to fulfill the test once,
> here is a way without a loop, using a list comprehension.

A list comprehension *is* a loop. It even includes a "for" inside it.


I really don't understand why people so often say things like "I have a bunch of stuff to do repeatedly, but I want to do it without a loop". To put it another way, "I want to repeat something without repeating it". WTF???

The only way to avoid a loop *somewhere* is to have a parallel-processing computer with at least as many parallel processes as you have things to repeat. So if Hs has 300K lines to process, he would need 300K processors, one per line. Since that's impractical unless you're Google or the NSA[1] you're going to need a loop, the only question is whether it is an *explicit* loop or an *implicit* loop.

For example, a standard for-loop:

for line in many_lines:
     process(line)


or a list-comprehension:

[process(line) for line in many_lines]


are explicit loops. The map built-in is implicit:

map(process, many_lines)

So are many of numpy's array functions. But regardless of whether *you* write the loop, or Python does it for you, there is still a loop.





[1] Hi guys!


-- 
Steven


More information about the Tutor mailing list