In this “list contains python” best post i learn to about different methods to check if a given ProductId exists in list or not using Python.
How to Check if an item exists in list ?
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; }
Also Read: python list contains
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
Also Read: convert string to array python
Also Read: Shell Script to Add Two Numbers using ommand line arguments
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 split a string in python? | python split string into list
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
Also Read: python developer resume Examples
Also Read: How to Use Time Sleep in Python Script?
Also Read: python unzip
Also Read: Python Array Contains: How To Check If Item Exists In Array?
Also Read: How to create a Directory in python ?