How To Find Object In A List Of Objects In Python

This article will share with you how to find objects in a List of objects in Python. To do that, you can use the next() function or iterate the list and check which element accommodates the conditions and return it. Follow us to learn more about it with the explanation and examples below.

Find Object In A List Of Objects In Python

Find an object in the list with the next() function

To find an object in the list, you can use the next() function, it will return the first result that accommodates the conditions. If no element accommodates the conditions, the result will be None.

Look at the example below.

class Player:
    def __init__(self, full_name= '', club = '', age = int(), goals = int()):
        self.full_name = full_name
        self.club = club
        self.age = age
        self.goals = goals
     
    # Override
    def __str__(self):
        return self.full_name + ': ' + self.club + ', Age: ' + str(self.age) + ', Goals: ' + str(self.goals)
if __name__ == '__main__':
    # Create four objects.
    player1 = Player('Cr.Ronaldo', 'Real Madrid', 37, 819)
    player2 = Player('L.Messi', 'Paris Saint-Germain', 35, 789)
    player3 = Player('H.Maguire', 'Manchester United', 30, 25)
    player4 = Player('G.Bale', 'Real Madrid', 33, 295)

    # Add these objects to the new list.
    player_list = [player1, player2, player3, player4]

    # Find the first player in this list whose club is Real Madrid.
    result = next(
        (player for player in player_list if player.club == 'Real Madrid'), None
    )
    print(result)

Output

Cr.Ronaldo: Real Madrid, Age: 37, Goals: 819

Find an object in the list with the loop

In this solution, we will find an object in the list with the loop and check the first element that accommodates the conditions. If no element accommodates the conditions, the result will be None.

Look at the example below.

class Player:
    def __init__(self, full_name= '', club = '', age = int(), goals = int()):
        self.full_name = full_name
        self.club = club
        self.age = age
        self.goals = goals

    # Override
    def __str__(self):
        return self.full_name + ': ' + self.club + ', Age: ' + str(self.age) + ', Goals: ' + str(self.goals)
if __name__ == '__main__':
    # Create four objects.
    player1 = Player('Cr.Ronaldo', 'Real Madrid', 37, 819)
    player2 = Player('L.Messi', 'Paris Saint-Germain', 35, 789)
    player3 = Player('H.Maguire', 'Manchester United', 30, 25)
    player4 = Player('G.Bale', 'Real Madrid', 33, 295)

    # Add these objects to the new list.
    player_list = [player1, player2, player3, player4]

    # Find the first player in this list whose club is Real Madrid.
    result = None
    for player in player_list:
        if player.club == 'Real Madrid':
            result = player
            break
    print(result)

Output

Cr.Ronaldo: Real Madrid, Age: 37, Goals: 819

Find all objects in the list with the loop

To find all objects in the list with the loop, you can use the loop and return all elements that accommodate the conditions.

Look at the example below.

class Player:
    def __init__(self, full_name= '', club = '', age = int(), goals = int()):
        self.full_name = full_name
        self.club = club
        self.age = age
        self.goals = goals

    # Override
    def __str__(self):
        return self.full_name + ': ' + self.club + ', Age: ' + str(self.age) + ', Goals: ' + str(self.goals)
if __name__ == '__main__':
    # Create four objects.
    player1 = Player('Cr.Ronaldo', 'Real Madrid', 37, 819)
    player2 = Player('L.Messi', 'Paris Saint-Germain', 35, 789)
    player3 = Player('H.Maguire', 'Manchester United', 30, 25)
    player4 = Player('G.Bale', 'Real Madrid', 33, 295)

    # Add these objects to the new list.
    player_list = [player1, player2, player3, player4]

    # Find the all players in this list whose club is Real Madrid.
    result = []
    for player in player_list:
        if player.club == 'Real Madrid':
            result.append(player)

    # Print the result.
    for element in result:
        print(element)

Output

Cr.Ronaldo: Real Madrid, Age: 37, Goals: 819
G.Bale: Real Madrid, Age: 33, Goals: 295

Summary

We have shared with you how to find objects in a List of Objects in Python. To do that, you can use the next() function and the loop. We recommend you use the loop because it is easy to customize and also finds all objects in the list. We hope this tutorial is helpful to you. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *