In Python, you can add two lists together using the +
operator. This concatenates the lists, creating a new list containing the elements of both lists. Here’s an example:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list1 + list2
print(result)
Output:
[1, 2, 3, 4, 5, 6]
This adds the elements of list2
to the end of list1
, creating a new list.
Alternative: Using extend()
You can also use the extend()
method to add elements of one list to another. This modifies the original list in place:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)
Output:
[
]
This method modifies list1
by adding the elements of list2
to it.