Open In App

Searching a list of objects in Python

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Searching for a single or group of objects can be done by iterating through a list.

You might want to search a list of objects to find the object in the list of objects. It is very hard to look for objects manually, you can use the below-discussed method to search for objects in a list. You can also use conditions to filter objects from list of objects.

In this article, we will look at ways to find objects in a list of objects in Python.

Syntax to Search in a List of Objects:

class_name.object_name

where,

  • class_name is the name of the class
  • object_name is the name of the object

Examples on Search a list of Objects

We have provided multiple examples to search objects in a list of objects in Python.

Example 1:

Create a class Car with the following attributes and perform a search operation that returns cars with a price less than 10 Lakhs (10,00,000/-).

Attributes:

  • String company
  • String modelName
  • int price
  • int seatingCapacity

Python3




class Car():
   
    # constructor
    def __init__(self, company, modelName, price, seatingCapacity):
        self.company = company
        self.modelName = modelName
        self.price = price
        self.seatingCapacity = seatingCapacity
 
 
# list of car objects
carsList = [Car('Honda', 'Jazz', 900000, 5),
            Car('Suzuki', 'Alto', 450000, 4),
            Car('BMW', 'X5', 9000000, 5)]
 
# cars with price less than 10 Lakhs
economicalCars = [car for car in carsList if car.price <= 1000000]
 
# print those cars
for car in economicalCars:
    print(car.company+'--'+car.modelName)


Output

Honda--Jazz
Suzuki--Alto

Example 2

Use the same Car class and search for cars which is having a seating capacity as 4.

Python3




class Car():
   
    # constructor
    def __init__(self, company, modelName, price, seatingCapacity):
        self.company = company
        self.modelName = modelName
        self.price = price
        self.seatingCapacity = seatingCapacity
 
 
# list of car objects
carsList = [Car('Honda', 'Jazz', 900000, 5),
            Car('Suzuki', 'Alto', 450000, 4),
            Car('BMW', 'X5', 9000000, 5)]
 
# cars having seating capacity 4
smallCars = [car for car in carsList if car.seatingCapacity == 4]
 
# print those cars
for car in smallCars:
    print(car.company+'--'+car.modelName+'--'+str(car.seatingCapacity))


Output

Suzuki--Alto--4

Example 3:

Use the above same Car class, search for BMW company cars and return them.

Python3




class Car():
   
    # constructor
    def __init__(self, company, modelName, price, seatingCapacity):
        self.company = company
        self.modelName = modelName
        self.price = price
        self.seatingCapacity = seatingCapacity
 
 
# list of car objects
carsList = [Car('Honda', 'Jazz', 900000, 5),
            Car('Suzuki', 'Alto', 450000, 4),
            Car('BMW', 'X5', 9000000, 5)]
 
# bmw cars
BMW_Cars = [car for car in carsList if car.company == 'BMW']
 
# print those cars
for car in BMW_Cars:
    print(car.company+'--'+car.modelName+'--' +
          str(car.price)+'--'+str(car.seatingCapacity))


Output

BMW--X5--9000000--5

In this article, we have given 3 examples and explained how to search for an object in a list of objects. Python class offers very basic functionality if you want to find object, it can return multiple objects if they meet the search criterion.

Search operations are very important and useful in every concept.

Similar Reads:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads