DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 33 of 77
←PreviousPrevNextNext→

Join Lists

Concatenation Methods

Python provides multiple ways to combine lists. You can use the + operator to create a new list, extend() to modify in place, itertools.chain() for efficient iteration, unpacking syntax for readability, and sum() with an empty list as the starting value.

Example
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# + operator (creates new list)
combined = list1 + list2

# extend() modifies in place
list1.extend(list2)

# itertools.chain (efficient iterator)
from itertools import chain
chained = list(chain(list1, list2))

# Unpacking syntax
unpacked = [*list1, *list2]

print(combined)
print(list1)
print(chained)
print(unpacked)
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Performance Comparison

Although many methods exist, their performance differs. extend() is typically fastest for in-place updates, while + and unpacking create new lists. itertools.chain() is memory-efficient for large sequences since it returns an iterator.

Example
from timeit import timeit

print("+ operator:", timeit("x + y", "x = [1,2,3]; y = [4,5,6]"))
print("extend():", timeit("x.extend(y)", "x = [1,2,3]; y = [4,5,6]"))
print("unpacking:", timeit("[*x, *y]", "x = [1,2,3]; y = [4,5,6]"))
ℹ️ Note: Use extend() for efficiency when modifying an existing list, and itertools.chain() for memory-efficient iteration across many lists.

Advanced Joining

For more complex scenarios, such as joining multiple lists or flattening nested structures, different techniques can be applied. The sum() trick works for shallow joins, while recursion is often used for deep flattening.

Example
# Joining multiple lists
lists = [[1, 2], [3, 4], [5, 6]]
joined = sum(lists, [])
print(joined)  # [1, 2, 3, 4, 5, 6]

# Flattening nested lists
nested = [[1, 2], [3, [4, 5]]]
from collections.abc import Iterable

def flatten(lst):
    for item in lst:
        if isinstance(item, Iterable) and not isinstance(item, (str, bytes)):
            yield from flatten(item)
        else:
            yield item

flat = list(flatten(nested))
print(flat)  # [1, 2, 3, 4, 5]
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5]
Test your knowledge: Join Lists
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 33 of 77
←PreviousPrevNextNext→