4 different ways in Python to create a list of objects

Python example to create a list of objects :

Python list can hold a list of class objects. We can create one empty list and append multiple class objects to this list. Each list element will be an object, and we can access any member of that object like method, variables, etc. Note that you can append different class objects to the same list.

In this post, I will show you how to create one list of objects in Python and also how to append items to a list of objects.

How to add items to a list:

We have different methods to add items to a list.

append() method:

The append method takes one item as its parameter and adds it to the end of the list. For example,

l = []
l.append(1)
l.append(2)
l.append(3)

print(l)

In this example, we created an empty list l and appended three numbers to the list. It will add these numbers to the end of the list l.

[1, 2, 3]

insert() method:

The insert method inserts an item into a list at any given position. The syntax of the insert method is:

insert(index, item)

Let’s take a look at the following example:

l = [1, 2, 3, 4, 5]
l.insert(1, -1)

print(l)

It will insert -1 at index position 1 of the list l. It will shift the other elements to the right. The above example will print [1, -1, 2, 3, 4, 5].

extend() method:

The extend method can add any iterable to a list. It takes the other iterable as its parameter and appends it to the original list.

For example:

l = [1, 2, 3, 4, 5]
l1 = [6, 7, 8]

l.extend(l1)

print(l)

It will print [1, 2, 3, 4, 5, 6, 7, 8].

Example 1: How to create a list of objects with the same class instance:

Let’s take a look at the following program:

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age


studentList = []
studentList.append(Student("Alex", 20))
studentList.append(Student("Bob", 21))
studentList.append(Student("Ira", 15))

for student in studentList:
    print("Name : {}, Age : {}".format(student.name, student.age))

In this example, we are appending objects to a list. The Student is a class with two properties name and age. The constructor of the Student class takes the name and age properties as its parameters and initializes one Student object.

First of all, we are initializing one empty list studentList and appending three different Student objects to this list. We are using the append method to append the items to the list.

The for loop is used to print the properties of the list objects.

It will print the below output :

Name : Alex, Age : 20
Name : Bob, Age : 21
Name : Ira, Age : 15

Python list objects

Get the code on Github

Example 2: list of objects with different class instances:

In the above example, we appended different objects of the same class to a list. We can also append objects of different classes to a list.

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

class Subject:
    def __init__(self, name):
        self.subjectName = name

data = []
data.append(Student("Alex", 20))
data.append(Subject("Subject-A"))
data.append(Student("Bob", 21))
data.append(Subject("Subject-B"))
data.append(Student("Ira", 15))

for item in data:
    if(isinstance(item, Student)):
        print('Name : {}, Age : {}'.format(item.name, item.age))
    else:
        print('Subject : {}'.format(item.subjectName))

In this example, we have created two different classes Student and Subject. But we are appending objects of both of these classes to the same list data. The for loop is checking the type of the object before printing out its content.

It will produce the below output :

Name : Alex, Age : 20
Subject : Subject-A
Name : Bob, Age : 21
Subject : Subject-B
Name : Ira, Age : 15

Get the code on Github

Method 3: Create a list of objects with the extend method:

We can pass one list of objects to the extend function to append the items to another list. We can use the extend function with an empty list to create one list of objects.

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age


l = []
data = [Student("Alex", 20), Student("Bob", 21), Student("Ira", 15)]
l.extend(data)

for item in l:
    print("Name : {}, Age : {}".format(item.name, item.age))

It will append the items of the list data to the list l. The for loop will print the name and age of the list items.

It will print:

Name : Alex, Age : 20
Name : Bob, Age : 21
Name : Ira, Age : 15

Get the code on Github

Method 4: Create a list of objects with the insert method:

The insert method can also be used to insert multiple items into an empty list. For example,

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age


l = []
l.insert(0, Student("Alex", 20))
l.insert(1, Student("Bob", 21))
l.insert(2, Student("Ira", 15))

for item in l:
    print("Name : {}, Age : {}".format(item.name, item.age))

The insert method is inserting the Student objects to the list l. If you run this program, it will print the below output:

Name : Alex, Age : 20
Name : Bob, Age : 21
Name : Ira, Age : 15

Get the code on Github

Similar tutorials :