Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to check if a value exists in a list

What is the fastest way to know if a value exists in a list (a list with millions of values in it) and what its index is?

I know that all values in the list are unique as in this example.

The first method I try is (3.8 sec in my real code):

a = [4,2,3,1,5,6]  if a.count(7) == 1:     b=a.index(7)     "Do something with variable b" 

The second method I try is (2x faster: 1.9 sec for my real code):

a = [4,2,3,1,5,6]  try:     b=a.index(7) except ValueError:     "Do nothing" else:     "Do something with variable b" 

Proposed methods from Stack Overflow user (2.74 sec for my real code):

a = [4,2,3,1,5,6] if 7 in a:     a.index(7) 

In my real code, the first method takes 3.81 sec and the second method takes 1.88 sec. It's a good improvement, but:

I'm a beginner with Python/scripting, and is there a faster way to do the same things and save more processing time?

More specific explanation for my application:

In the Blender API I can access a list of particles:

particles = [1, 2, 3, 4, etc.] 

From there, I can access a particle's location:

particles[x].location = [x,y,z] 

And for each particle I test if a neighbour exists by searching each particle location like so:

if [x+1,y,z] in particles.location     "Find the identity of this neighbour particle in x:the particle's index     in the array"     particles.index([x+1,y,z]) 
like image 867
Jean-Francois Gallant Avatar asked Sep 27 '11 15:09

Jean-Francois Gallant


People also ask

How do I check if a value exists in a list?

We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list.

How do you check if a string is present in list Python?

We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1. count(s) if count > 0: print(f'{s} is present in the list for {count} times.

How do you check if an array contains a value Python?

To check if an array contains an element or not in Python, use the in operator. The in operator checks whether a specified element is an integral element of a sequence like string, array, list, tuple, etc.

How do I find a specific element in a list Python?

To find an element in the list, use the Python list index() method, The index() is an inbuilt Python method that searches for an item in the list and returns its index. The index() method finds the given element in the list and returns its position.


1 Answers

7 in a 

Clearest and fastest way to do it.

You can also consider using a set, but constructing that set from your list may take more time than faster membership testing will save. The only way to be certain is to benchmark well. (this also depends on what operations you require)

like image 56
Rafe Kettler Avatar answered Sep 29 '22 06:09

Rafe Kettler