How to concatenate two lists in Python
Published on Aug. 22, 2023, 12:15 p.m.
To concatenate two lists in Python, you can use the concatenation operator +
or the extend()
method. Here are examples of both methods:
Using the +
operator:
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
concatenated_list = list1 + list2
print(concatenated_list) # outputs [1, 2, 3, 'a', 'b', 'c']
Using the extend()
method:
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
list1.extend(list2)
print(list1) # outputs [1, 2, 3, 'a', 'b', 'c']
Both methods will combine the two lists into a single list. Keep in mind that using +
operator will return a new list that is the concatenation of the two lists, while the extend()
method will modify the first list with the elements of the second list.