Use the + operator to Combine two lists in Python. There are many ways to join or concatenate, two or more lists in Python. Like, append method and extent method.
Example Combine two lists in Python
Simple example code.
Using the + operator
This is the easiest and most popular way to add 2 lists in Python.
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Output:
Append method+ for loop
Another way is appending all the items to another list using the for-loop and append method.
list1 = ["A", "B", "C"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
Output: [‘A’, ‘B’, ‘C’, 1, 2, 3]
Using extend() method
Extend() function will add an item at the end of another list.
list1 = ["A", "B", "C"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
Output: [‘A’, ‘B’, ‘C’, 1, 2, 3]
How do I concatenate two lists in Python?
As of 3.9, these are the most popular stdlib methods for concatenating two (or more) lists in python.
Source: stackoverflow.com
Do comment if you have any doubts and suggestions on this Python list code.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.