Check if a Number is in a List in Python

here I give you best example for Fastest way to check if a value exists in a list using Python.

Suppose you mean python “list” contains python where you say “array”, you can do

if item in my_list:
# whatever

Check if object with specific value exists in List

if(productListFirst.Any(a => a.Id == stringID))
{
//Your some Python logic goes here;
}

Python Check If List Item Exists

use the in keyword:
here in this example to Check if “pakainfo” is Existing in the list contains python:

thislist = ["pakainfo", "4cgandhi", "infinityknow"]
if "pakainfo" in thislist:
print("Yes, 'pakainfo' is in the Website list")

Also Read: Linear Search in C, C++, JAVA, PHP, Python3 and C#

With any Example

productListFirst = [[-9, -1, 3], [11, -8],[-4,434,0]]
search_ProductId = -8

# Given list
print("Given List :\n", productListFirst)

print("ProductId to Find: ",search_ProductId)

# Using in
if any(search_ProductId in sublist for sublist in productListFirst):
print("Existing")
else:
print("Not Existing")

Output

('Given List :\n', [[-9, -1, 3], [11, -8], [-4, 434, 0]])
('ProductId to Find: ', -8)
Existing

With in Example

productListFirst = [[-9, -1, 3], [11, -8],[-4,434,0]]
search_ProductId = -8

# Given list
print("Given List :\n", productListFirst)

print("ProductId to Find: ",search_ProductId)

# Using in
if search_ProductId in (item for sublist in productListFirst for item in sublist):
print("Existing")
else:
print("Not Existing")

search_ProductId = 13
print("New ProductId to Find: ",search_ProductId)

# Using in
if search_ProductId in (item for sublist in productListFirst for item in sublist):
print("Existing")
else:
print("Not Existing")

Output

Given List :
[[-9, -1, 3], [11, -8], [-4, 434, 0]]
ProductId to Find: -8
Existing
New ProductId to Find: 13
Not Existing

Also Read: how to skip a line in python | python skip lines starting with #

With chain Example

from itertools import chain

productListFirst = [[-9, -1, 3], [11, -8],[-4,434,0]]
search_ProductId = -8

# Given list
print("Given List :\n", productListFirst)

print("ProductId to Find: ",search_ProductId)

# Using in
if search_ProductId in chain(*productListFirst):
print("Existing")
else:
print("Not Existing")

search_ProductId = 13
print("New ProductId to Find: ",search_ProductId)

# Using in
if search_ProductId in chain(*productListFirst):
print("Existing")
else:
print("Not Existing")

Output

Given List :
[[-9, -1, 3], [11, -8], [-4, 434, 0]]
ProductId to Find: -8
Existing
New ProductId to Find: 13
Not Existing

#py. #python 

 Check if a Number is in a List in Python
1.00 GEEK