fast list lookup

K

Klaus Neuner

Hello,

what is the fastest way to determine whether list l (with
len(l)>30000) contains a certain element?


Klaus
 
S

Simon Brunning

what is the fastest way to determine whether list l (with
len(l)>30000) contains a certain element?

If the list isn't sorted, I doubt you'll do better than

if an_element in my_list:
# do whatever

If the list is sorted, have a look at the bisect module.
 
A

Alex Martelli

Klaus Neuner said:
what is the fastest way to determine whether list l (with
len(l)>30000) contains a certain element?

"if thecertainelement in l:"

is the fastest way unless there are properties of l which you're not
telling us about, or unless what you need is not just to determine
whether l contains a certain element but rather something different
(such as doing the same determination for several elements on an
unchanging l).


Alex
 
K

Kent Johnson

Klaus said:
Hello,

what is the fastest way to determine whether list l (with
len(l)>30000) contains a certain element?

If you can use a set or dict instead of a list this test will be much faster.

Kent
 
M

Marco Aschwanden

what is the fastest way to determine whether list l (with
Either a sorted list (in conjunction with the bisect-module) or a
dictionary is your friend...

Regards,
Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top