Check If Value Exists in Python List of Objects
Last Updated :
12 Feb, 2024
In Python, checking if a specific value exists in a list of objects is a common task. There are several methods to achieve this, and in this article, we will explore five generally used methods with simple code examples. Each method provides a different approach to checking for the existence of a value in a list of objects.
To Check If Value Exists in Python List Of Objects
Below, are the ways To Check If Value Exists In Python List Of Objects in Python.
- Using a For Loop
- Using
filter
Function
- Using
lambda
() Function
Create a List of Objects
In this example, the below code defines a `Person` class with attributes for name and age. An array `people` is then created, containing instances of the `Person` class, and the type of the `people` array is printed, resulting in “<class ‘list’>”.
Python3
class Person:
def __init__( self , name, age):
self .name = name
self .age = age
people = [Person( "Alice" , 25 ), Person( "Bob" , 30 ), Person( "Charlie" , 22 )]
print ( type (people))
|
Check If Value Exists In Python Using a For Loop
In this example, below code iterates through the list of people and checks if there is a person with age 30. If found, it prints “A person with age 30 exists in the list,” otherwise, it prints “No person with age 30 found in the list.”
Python3
target_age = 30
found = False
for person in people:
if person.age = = target_age:
found = True
break
if found:
print (f "A person with age {target_age} exists in the list." )
else :
print (f "No person with age {target_age} found in the list." )
|
Output
A person with age 30 exists in the list.
Check If Value Exists In Python Using filter()
Function
In this example, below code employs the `filter` function with a lambda expression to create an iterable of people with age 30. The `any` function then determines if there’s at least one person in the filtered list, and it prints either “A person with age 30 exists in the list.”
Python3
target_age = 30
filtered_people = filter ( lambda person: person.age = = target_age, people)
found = any (filtered_people)
if found:
print (f "A person with age {target_age} exists in the list." )
else :
print (f "No person with age {target_age} found in the list." )
|
Output
A person with age 30 exists in the list.
Check If Value Exists In Python Using lambda
() Function
In this example, below code utilizes the `map` function with a lambda expression to create an iterable of boolean values indicating whether each person’s age is 30. The `any` function then checks if at least one of these values is True, and it prints either “A person with age 30 exists in the list.”
Python3
target_age = 30
found = any ( map ( lambda person: person.age = = target_age, people))
if found:
print (f "A person with age {target_age} exists in the list." )
else :
print (f "No person with age {target_age} found in the list." )
|
Output
A person with age 30 exists in the list.
Share your thoughts in the comments
Please Login to comment...