Check if any element in list python

Python list contains: How to check if an item exists in list?

In Python, lists are important containers as they store all kinds of data types as a collection. It can contain up to 536,870,912 items in a 32-bit system. Sometimes it is difficult to know if the list contains a particular item. Python has various ways by which we can find out if the list contains the item.

Table of contents

Python lists contain — Introduction

Checking if an element is present in a list is one of the basic list operations in Python and there are many different ways we can check that. In this tutorial, we will be covering some of the ways to check if the lists contain an element.

Читайте также:  Python index for array

Check if the Python list contains an element using in operator

The most convenient way to check whether the list contains the element is using the in operator. Without sorting the list in any particular order, it returns TRUE if the element is there, otherwise FALSE.

The below example shows how this is done by using ‘in’ in the if-else statement.

Input:

list = [Adam, Dean, Harvey, Mick, John] if 'John' in list: print (" 'John' is found in the list") else print (" 'John' is not found in the list") if 'Ned' in list: print (" 'Ned' is found in the list") else print (" 'Ned' is not found in the list")

Output:

'John' is found in the list 'Ned' is not found in the list

Using for loop to check if the list contains an element in Python

Another simple method to check if the list contains the element is looping through it. As the name suggests, the loop will match each element of the list with the element that we are looking for one by one and will only stop if there’s a match or there is no match at all. The below example illustrates this.

Input:

list = [Adam, Dean, Harvey, Mick, John] for name in list: if name == 'Adam': print ("Found the element")

Output:

Using any() to check if the list contains

The function any() is a built-in approach that checks for a match in a string with a match of each list element.

The below example shows how the any() function works. We check if there are any common items in the string, ‘Adam lives in New York’, and the list mentioned in the first line.

Читайте также:  Compared to date in javascript

Input:

list = [Adam, Dean, Harvey, Mick, John] string = "Adam lives in New York" print ("The original list is: " + str(list)) print ("The original string is: " + string) result = any(item in string for item in list) print ("Does the string contain 'Adam': " + str(result))

Output:

The original list is: [Adam, Dean, Harvey, Mick, John] The original string is: Adam lives in New York Does the string contain 'Adam': True 

count() to check if the list contains

Another built-in method in Python, count() returns the number of times the passed element occurs in the list. If the element is not there in the list then the count() will return 0. If it returns a positive integer greater than 0, it means the list contains the element.

Input:

list = [Adam, Dean, Harvey, Mick, John] result = list.count(Harvey) if result > 0: print("Harvey exists in the list") else: print("Harvey does not exist in the list")

Output:

Harvey exists in the list

Closing thoughts

In this tutorial, we have used the ‘in’ operator, for loop, any() and count() methods to check whether a particular item exists in the list or not. One can learn more about other concepts related to Python here.

Источник

Python : How to Check if an item exists in list ? | Search by Value or Condition

In this article we will discuss different ways to check if a given element exists in list or not.

Table of Contents

Introduction

Suppose we have a list of strings i.e.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from']

Now let’s check if given list contains a string element ‘at’ ,

Check if element exists in list using python “in” Operator

Condition to check if element is in List :

It will return True, if element exists in list else return false.

Frequently Asked:

For example check if ‘at’ exists in list i.e.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # check if element exist in list using 'in' if 'at' in listOfStrings : print("Yes, 'at' found in List : " , listOfStrings)

Condition to check if element is not in List :

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # check if element NOT exist in list using 'in' if 'time' not in listOfStrings : print("Yes, 'time' NOT found in List : " , listOfStrings)

Check if element exist in list using list.count() function

count(element) function returns the occurrence count of given element in the list. If its greater than 0, it means given element exists in list.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # check if element exist in list using count() function if listOfStrings.count('at') > 0 : print("Yes, 'at' found in List : " , listOfStrings)

Check if element exist in list based on custom logic

Python any() function checks if any Element of given Iterable is True.

Let’s use it to check if any string element in list is of length 5 i.e.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # Check if element exist in list based on custom logic # Check if any string with length 5 exist in List result = any(len(elem) == 5 for elem in listOfStrings) if result: print("Yes, string element with size 5 found")

Instead of condition we can use separate function in any to match the condition i.e.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] def checkIfMatch(elem): if len(elem) == 5: return True; else : return False; # Check if any string that satisfies the condition # in checkIfMatch() function exist in List result = any(checkIfMatch for elem in listOfStrings)

Complete example is as follows,

def checkIfMatch(elem): if len(elem) == 5: return True; else : return False; # List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # Print the List print(listOfStrings) ''' check if element exist in list using 'in' ''' if 'at' in listOfStrings : print("Yes, 'at' found in List : " , listOfStrings) ''' check if element NOT exist in list using 'in' ''' if 'time' not in listOfStrings : print("Yes, 'time' NOT found in List : " , listOfStrings) ''' check if element exist in list using count() function ''' if listOfStrings.count('at') > 0 : print("Yes, 'at' found in List : " , listOfStrings) ''' check if element exist in list based on custom logic Check if any string with length 5 exist in List ''' result = any(len(elem) == 5 for elem in listOfStrings) if result: print("Yes, string element with size 5 found") ''' Check if any string that satisfies the condition in checkIfMatch() function exist in List ''' result = any(checkIfMatch for elem in listOfStrings) if result: print("Yes, string element with size 5 found")
['Hi', 'hello', 'at', 'this', 'there', 'from'] Yes, 'at' found in List : ['Hi', 'hello', 'at', 'this', 'there', 'from'] Yes, 'time' NOT found in List : ['Hi', 'hello', 'at', 'this', 'there', 'from'] Yes, 'at' found in List : ['Hi', 'hello', 'at', 'this', 'there', 'from'] Yes, string element with size 5 found Yes, string element with size 5 found

Источник

Python : How to Check if an item exists in list ? | Search by Value or Condition

In this article we will discuss different ways to check if a given element exists in list or not.

Table of Contents

Introduction

Suppose we have a list of strings i.e.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from']

Now let’s check if given list contains a string element ‘at’ ,

Check if element exists in list using python “in” Operator

Condition to check if element is in List :

It will return True, if element exists in list else return false.

Frequently Asked:

For example check if ‘at’ exists in list i.e.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # check if element exist in list using 'in' if 'at' in listOfStrings : print("Yes, 'at' found in List : " , listOfStrings)

Condition to check if element is not in List :

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # check if element NOT exist in list using 'in' if 'time' not in listOfStrings : print("Yes, 'time' NOT found in List : " , listOfStrings)

Check if element exist in list using list.count() function

count(element) function returns the occurrence count of given element in the list. If its greater than 0, it means given element exists in list.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # check if element exist in list using count() function if listOfStrings.count('at') > 0 : print("Yes, 'at' found in List : " , listOfStrings)

Check if element exist in list based on custom logic

Python any() function checks if any Element of given Iterable is True.

Let’s use it to check if any string element in list is of length 5 i.e.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # Check if element exist in list based on custom logic # Check if any string with length 5 exist in List result = any(len(elem) == 5 for elem in listOfStrings) if result: print("Yes, string element with size 5 found")

Instead of condition we can use separate function in any to match the condition i.e.

# List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] def checkIfMatch(elem): if len(elem) == 5: return True; else : return False; # Check if any string that satisfies the condition # in checkIfMatch() function exist in List result = any(checkIfMatch for elem in listOfStrings)

Complete example is as follows,

def checkIfMatch(elem): if len(elem) == 5: return True; else : return False; # List of string listOfStrings = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # Print the List print(listOfStrings) ''' check if element exist in list using 'in' ''' if 'at' in listOfStrings : print("Yes, 'at' found in List : " , listOfStrings) ''' check if element NOT exist in list using 'in' ''' if 'time' not in listOfStrings : print("Yes, 'time' NOT found in List : " , listOfStrings) ''' check if element exist in list using count() function ''' if listOfStrings.count('at') > 0 : print("Yes, 'at' found in List : " , listOfStrings) ''' check if element exist in list based on custom logic Check if any string with length 5 exist in List ''' result = any(len(elem) == 5 for elem in listOfStrings) if result: print("Yes, string element with size 5 found") ''' Check if any string that satisfies the condition in checkIfMatch() function exist in List ''' result = any(checkIfMatch for elem in listOfStrings) if result: print("Yes, string element with size 5 found")
['Hi', 'hello', 'at', 'this', 'there', 'from'] Yes, 'at' found in List : ['Hi', 'hello', 'at', 'this', 'there', 'from'] Yes, 'time' NOT found in List : ['Hi', 'hello', 'at', 'this', 'there', 'from'] Yes, 'at' found in List : ['Hi', 'hello', 'at', 'this', 'there', 'from'] Yes, string element with size 5 found Yes, string element with size 5 found

Источник

Оцените статью