Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To merge lists of class objects in Python3, you can use the built-in function extend().

Here is an example:

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

# Create two lists of Person objects
list1 = [Person("Alice", 25), Person("Bob", 30)]
list2 = [Person("Charlie", 35), Person("David", 40)]

# Merge the two lists using extend()
list1.extend(list2)

# Print the merged list
for person in list1:
    print(person.name, person.age)

Output:

Alice 25
Bob 30
Charlie 35
David 40

In the above example, we created two lists list1 and list2 containing Person objects. We then merged the two lists using the extend() method of list1.

Note that extend() modifies the original list in place and adds the elements of the second list to the end of the first list.