[Tutor] dictionaries help

Kent Johnson kent37 at tds.net
Fri Jul 24 20:40:49 CEST 2009


On Fri, Jul 24, 2009 at 3:01 AM, <davidwilson at safe-mail.net> wrote:
>> If ws_industry contains 25 items, it will probably be faster to search
>> it if it is a set rather than a list. Just use
>> ws_industry = set(('it', 'science'))
>
> ws_industry contains only unique values

OK. It will still be faster to check membership if it is a set rather
than a list.

Checking membership in a list has to search the whole list in order.
The time will vary depending on where, and if, the target value is
found in the list:
kent $ python -m timeit -s "m=map(str, range(25))" "'x' in m"
1000000 loops, best of 3: 0.902 usec per loop

kent $ python -m timeit -s "m=map(str, range(25))" "'12' in m"
1000000 loops, best of 3: 0.649 usec per loop

kent $ python -m timeit -s "m=map(str, range(25))" "'0' in m"
10000000 loops, best of 3: 0.0801 usec per loop

Searching a set takes approximately the same amount of time regardless
of the size of the set and whether the target is in the set or not:
kent $ python -m timeit -s "m=set(map(str, range(25)))" "'x' in m"
10000000 loops, best of 3: 0.0854 usec per loop

kent $ python -m timeit -s "m=set(map(str, range(25)))" "'12' in m"
10000000 loops, best of 3: 0.117 usec per loop

kent $ python -m timeit -s "m=set(map(str, range(25)))" "'0' in m"
10000000 loops, best of 3: 0.0867 usec per loop

Notice that the first two tests are significantly faster than
searching the list; the last one is slightly slower but this is the
best case for the list search (finding the first element).

Kent


More information about the Tutor mailing list