To concatenate two lists of different sizes in Python, you can use the extend
method or the +
operator. Here are examples of both approaches:
- Using the
extend
method:
list1 = [1, 2, 3]
list2 = [4, 5, 6, 7]
list1.extend(list2)
print(list1)
- Using the
+
operator:
list1 = [1, 2, 3]
list2 = [4, 5, 6, 7]
concatenated_list = list1 + list2
print(concatenated_list)
Both approaches will produce the same result. The extend
method modifies the original list1
in place, whereas the +
operator creates a new list and assigns it to the concatenated_list
variable. You can choose the approach that suits your needs.
+ There are no comments
Add yours