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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-14 09:56:59 +0000
Seen: 8 times
Last updated: Jul 14 '23
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?