Open In App

How to create a list of object in Python class

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, creating a list of objects involves storing instances of a class in a list, which allows for easy access, modification and iteration. For example, with a Geeks class having attributes name and roll, you can efficiently manage multiple objects in a list. Let's explore different methods to do this efficiently.

Using list comprehension

List comprehension is an efficient way to create a new list by applying an expression to each item in an iterable. In this case, it's used to create a list of objects in a single line, making the code compact and readable. This method is considered the most Pythonic and is often the preferred approach for creating lists of objects.

Python
class Geeks:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll

# create list of objects
a = [Geeks(name, roll) for name, roll in [('Akash', 2), ('Deependra', 40), ('Reaper', 44), ('Veer', 67)]]

for obj in a:
    print(obj.name, obj.roll, sep=' ')

Output
Akash 2
Deependra 40
Reaper 44
Veer 67

Explanation:

  • List comprehension iterates over a list of tuples [(name, roll)] and creates a Geeks object for each tuple using Geeks(name, roll). The result is a list a that contains the Geeks objects.
  • For loop iterates over the list a, accessing each object's name and roll attributes and printing them with a space separator.

Using map()

map() function applies a given function to each item in an iterable and returns a map object, which can then be converted into a list. When used for object creation, map() applies the constructor function to each data item, creating objects efficiently.

Python
class Geeks:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll

# create list of objects
a = list(map(lambda x: Geeks(x[0], x[1]), [('Akash', 2), ('Deependra', 40), ('Reaper', 44), ('Veer', 67)]))


for obj in a:
    print(obj.name, obj.roll, sep=' ')

Output
Akash 2
Deependra 40
Reaper 44
Veer 67

Explanation:

  • List creation with map() uses map() and a lambda function to create Geeks objects from a list of tuples, converting it into a list.
  • For loop iterates over the list a, printing each object's name and roll attributes.

Using extend()

extend() method efficiently adds multiple objects to a list in a single step. It’s more efficient than appending each object individually, as it avoids the overhead of resizing the list multiple times.

Python
class Geeks:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll

# create list of objects
a = []
a.extend([Geeks('Akash', 2), Geeks('Deependra', 40), Geeks('Reaper', 44), Geeks('Veer', 67)])

for obj in a:
    print(obj.name, obj.roll, sep=' ')

Output
Akash 2
Deependra 40
Reaper 44
Veer 67

Explanation:

  • List creation with extend() add multiple Geeks objects to the list a in one step.
  • For loop iterates over the list a, printing the name and roll of each Geeks object with a space separator.

Using for loop

For loop allows you to iterate over a collection of data and append objects to a list manually. This approach gives you full control over the object creation process and is easy to understand, but it’s less efficient than the previous methods, especially for larger datasets.

Python
class Geeks:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll

# create list of objects
a = []
for data in [('Akash', 2), ('Deependra', 40), ('Reaper', 44), ('Veer', 67)]:
    a.append(Geeks(data[0], data[1]))

for obj in a:
    print(obj.name, obj.roll, sep=' ')

Output
Akash 2
Deependra 40
Reaper 44
Veer 67

Explanation:

  • First for loop iterates over a list of tuples, creating Geeks objects and adding them to the list a using append().
  • Second for loop iterates through the list a, printing each object's name and roll attributes.

Next Article
Practice Tags :

Similar Reads