Joining lists in Python means combining the elements of two or more lists into a single list. You can do this in multiple ways:
+ operator to create a new combined list.extend() method to add elements to an existing list.for loop with append() for manual merging.list1 + list2 returns a new list.list1.extend(list2) modifies list1.Using + (concatenation)
Creates a brand-new list, leaving the original lists unchanged.
result = list1 + list2
Using extend()
Modifies the existing list by appending all elements of another list.
list1.extend(list2)
Loops and list comprehensions give you more control, for example when you want to filter or transform elements while joining.
You can join two or more lists using the + operator.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
joined = list1 + list2
print(joined)
Use the extend() method to add all items of one list to another.
list1 = ["apple", "banana"]
list2 = ["cherry", "mango"]
list1.extend(list2)
print(list1)
You can append items from one list into another using a loop.
list1 = ["a", "b", "c"]
list2 = ["d", "e", "f"]
for item in list2:
list1.append(item)
print(list1)
List comprehensions can also be used to join lists, and optionally transform items.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
joined = [x for x in list1] + [y for y in list2]
print(joined)
list1 + list2 prints [1, 2, 3, 4, 5, 6].list1.extend(list2) where list1 = ["apple", "banana"] and list2 = ["cherry", "mango"], list1 becomes ["apple", "banana", "cherry", "mango"].["a", "b", "c", "d", "e", "f"] by appending each item of list2 into list1.[1, 2, 3, 4, 5, 6], but you could easily transform values, e.g. [x * 2 for x in list1].+ when you want a new combined list and to keep originals unchanged.extend() when you want to modify an existing list in-place.append() with a whole list if you want to merge elements; it will add the list as a single item.+ creates a new list, which can use extra memory for very large lists.+ operator and print the result.extend() to merge two lists of strings and observe how the first list changes.append().[2, 4, 6, 8, 10, 12] from [1, 2, 3] and [4, 5, 6].