# [Tutor] List comprehensions

W W srilyk at gmail.com
Thu Apr 10 13:31:59 CEST 2008

```My guess, though I'm not sure, is that google uses hashes...

why? Because they're a *ahem* load faster than loops, and the reason
is they replace the repetitive nature of a loop by using some type of
formula. Exactly /how/ this is implemented, I'm not sure.

A simple example of the speed difference:

11 * 1 =  11
The program spent 0.000810146331787 seconds.
11 * 1 =  11
The program spent 6.19888305664e-05 seconds.
(btw, that means .000006... )

The difference? The first was a loop:

1 from time import time
2 start_time = time()
3
4 x = 0
5 while x < 11:
6     x +=1
7
8 print "11 * 1 = ", x
9
10 end_time = time()
11 print "The program spent", end_time - start_time, "seconds."

And the second, a algebraic statement

14 start_time = time()
15
16 x = 11 / 1
17 print "11 * 1 = ", x
18
19 end_time = time()
20 print "The program spent", end_time - start_time, "seconds."

It would be simple to replace the 11 with a variable supplied by
something like variable = int(raw_input("Enter a number: "))
and you would come out with similar output.

That's basically the reason a dictionary finds dictionary["foo"]
faster than a for loop: the key, "foo", is transformed into some value
(As I understand it, the hashtable refers to some memory location, i.e
0x08f or some such), and there, sitting in that location, is the value
for the key "foo".

so rather than comparing each value a list, it would be like having
some formula to grab that value.

I hope this wasn't too confusing, and if anyone has any corrections or
clarifications, feel free to muck about.

But yeah, a hash is probably the way you want to go (from what I know)
-Wayne

On Thu, Apr 10, 2008 at 5:03 AM, linuxian iandsd <pylinuxian at gmail.com> wrote:
> also if you need to go for 20000 results I propose you use filters &
> & thus limit the query results.
>
> _______________________________________________
>  Tutor maillist  -  Tutor at python.org
>  http://mail.python.org/mailman/listinfo/tutor
>
>

--
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
```