How to Concatenate Two Lists in Python?

python

You can concatenate two lists in Python by using the "+" operator or by using the extend() method.

Using the "+" operator:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print(concatenated_list)
# Output: [1, 2, 3, 4, 5, 6]

Using the extend() method:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)
# Output: [1, 2, 3, 4, 5, 6]

Note that using the "+" operator creates a new list, whereas using the extend() method modifies the original list.

Latest Questions

python How to Fix ""zsh: command not found: python" Error on MacOS X? python How to Fix "xlrd.biffh.XLRDError: Excel xlsx file; not supported" Error in Pandas? python How to Remove All Whitespace From a Python String?