Saturday, November 16, 2024
Google search engine
HomeLanguagesSearching a list of objects in Python

Searching a list of objects in Python

In this article, we will discuss how to search a list of objects in Python.

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

Syntax:

class_name.object_name

where,

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

Example 1:

Create a class Car with the following attributes and perform a search operation that returns cars with 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
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments