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

Similar Threads

Simple question python 2
I Need Fix In Code 1
LEETCODE 3 3
Python Fast I/o 5
Range / empty list issues?? 1
Fast list traversal 4
Blue J Ciphertext Program 2
Improving the web page download code. 5

Members online

No members online now.

Forum statistics

Threads
474,218
Messages
2,571,123
Members
47,725
Latest member
Rudy

Latest Threads

Top