← Back to Chapters

Python Join Lists

? Python Join Lists

⚡ Quick Overview

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.
  • A for loop with append() for manual merging.
  • List comprehension when you want to transform items while joining.

? Key Concepts

  • Concatenation: list1 + list2 returns a new list.
  • In-place extension: list1.extend(list2) modifies list1.
  • Loop + append: useful when you need extra logic while merging.
  • List comprehension: lets you join and transform elements in one expression.

? Syntax and Theory

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.

? Code Examples

➕ Join Lists Using + Operator

You can join two or more lists using the + operator.

? View Code Example
list1 = [1, 2, 3]
list2 = [4, 5, 6]
joined = list1 + list2
print(joined)

? Join Lists Using extend() Method

Use the extend() method to add all items of one list to another.

? View Code Example
list1 = ["apple", "banana"]
list2 = ["cherry", "mango"]
list1.extend(list2)
print(list1)

? Join Lists Using a Loop

You can append items from one list into another using a loop.

? View Code Example
list1 = ["a", "b", "c"]
list2 = ["d", "e", "f"]

for item in list2:
    list1.append(item)

print(list1)

⚡ Join Lists Using List Comprehension

List comprehensions can also be used to join lists, and optionally transform items.

? View Code Example
list1 = [1, 2, 3]
list2 = [4, 5, 6]
joined = [x for x in list1] + [y for y in list2]
print(joined)

? Live Output and Explanation

  • list1 + list2 prints [1, 2, 3, 4, 5, 6].
  • After list1.extend(list2) where list1 = ["apple", "banana"] and list2 = ["cherry", "mango"], list1 becomes ["apple", "banana", "cherry", "mango"].
  • The loop example builds ["a", "b", "c", "d", "e", "f"] by appending each item of list2 into list1.
  • The list comprehension example also produces [1, 2, 3, 4, 5, 6], but you could easily transform values, e.g. [x * 2 for x in list1].

✅ Tips and Best Practices

  • Use + when you want a new combined list and to keep originals unchanged.
  • Use extend() when you want to modify an existing list in-place.
  • Use loops when you need complex logic while merging (conditions, extra processing, etc.).
  • Use list comprehensions when you want to transform items while joining (e.g. doubling or filtering).
  • Avoid using append() with a whole list if you want to merge elements; it will add the list as a single item.
  • Remember that + creates a new list, which can use extra memory for very large lists.

? Try It Yourself / Practice Tasks

  • Join two lists of numbers using the + operator and print the result.
  • Use extend() to merge two lists of strings and observe how the first list changes.
  • Write a loop that adds all items from one list into another using append().
  • Join two lists using list comprehension and double each element, e.g. make [2, 4, 6, 8, 10, 12] from [1, 2, 3] and [4, 5, 6].