Python Lists: Join Lists

Combining Python Lists

In Python, there are multiple approaches to joining, or concatenating, two or more lists. Each method offers flexibility depending on your specific needs.

Using the + Operator

One of the simplest methods to combine lists is by using the + operator. This approach creates a new list that contains all the elements from both lists.


list1 = ["The Hobbit", "1984", "Brave New World"]
list2 = [451, 732, 889]

list3 = list1 + list2
print(list3)
# Expected output: ['The Hobbit', '1984', 'Brave New World', 451, 732, 889]

Appending Items from One List to Another

Another technique involves appending each element from the second list to the first list individually. This method modifies the original list by adding elements from the second list one by one.


list1 = ["The Hobbit", "1984", "Brave New World"]
list2 = [451, 732, 889]

for x in list2:
    list1.append(x)

print(list1)
# Expected output: ['The Hobbit', '1984', 'Brave New World', 451, 732, 889]

Extending a List

The extend() method is another powerful way to join two lists. It appends all elements from the second list to the end of the first list, effectively combining them into a single list.


list1 = ["The Hobbit", "1984", "Brave New World"]
list2 = [451, 732, 889]

list1.extend(list2)
print(list1)
# Expected output: ['The Hobbit', '1984', 'Brave New World', 451, 732, 889]

Each of these methods allows you to join lists in Python, whether you need to create a new list or modify an existing one.

Leave a Comment