Python itertools.chain Function

The itertools.chain function in Python's itertools module returns an iterator that iterates over multiple iterables in sequence, as if they were a single iterable. It is useful for chaining together multiple sequences to be treated as one continuous sequence.

Table of Contents

  1. Introduction
  2. itertools.chain Function Syntax
  3. Examples
    • Basic Usage
    • Chaining Multiple Lists
    • Chaining Different Types of Iterables
    • Using chain.from_iterable
  4. Real-World Use Case
  5. Conclusion

Introduction

The itertools.chain function creates an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, and so on, until all of the iterables are exhausted. This can be useful for combining multiple sequences into a single sequence.

itertools.chain Function Syntax

Here is how you use the itertools.chain function:

import itertools

iterator = itertools.chain(*iterables)

Parameters:

  • *iterables: One or more iterables to be chained together.

Returns:

  • An iterator that yields elements from the input iterables, one after another.

Examples

Basic Usage

Chain two lists together and iterate over the combined sequence.

Example

import itertools

list1 = [1, 2, 3]
list2 = [4, 5, 6]
chained = itertools.chain(list1, list2)

print(list(chained))

Output:

[1, 2, 3, 4, 5, 6]

Chaining Multiple Lists

Chain multiple lists together.

Example

import itertools

list1 = [1, 2]
list2 = [3, 4]
list3 = [5, 6]
chained = itertools.chain(list1, list2, list3)

print(list(chained))

Output:

[1, 2, 3, 4, 5, 6]

Chaining Different Types of Iterables

Chain different types of iterables (e.g., lists, tuples, strings).

Example

import itertools

list1 = [1, 2, 3]
tuple1 = (4, 5)
string1 = '67'
chained = itertools.chain(list1, tuple1, string1)

print(list(chained))

Output:

[1, 2, 3, 4, 5, '6', '7']

Using chain.from_iterable

Use chain.from_iterable to chain iterables from a single iterable of iterables.

Example

import itertools

list_of_lists = [[1, 2], [3, 4], [5, 6]]
chained = itertools.chain.from_iterable(list_of_lists)

print(list(chained))

Output:

[1, 2, 3, 4, 5, 6]

Real-World Use Case

Processing Multiple Data Sources

Combine data from multiple sources (e.g., logs, data files) into a single sequence for processing.

Example

import itertools

logs1 = ['log1_line1', 'log1_line2']
logs2 = ['log2_line1', 'log2_line2']
logs3 = ['log3_line1', 'log3_line2']

all_logs = itertools.chain(logs1, logs2, logs3)

for line in all_logs:
    print(line)

Output:

log1_line1
log1_line2
log2_line1
log2_line2
log3_line1
log3_line2

Conclusion

The itertools.chain function is used for creating a single iterator from multiple iterables. Whether you need to combine lists, tuples, strings, or other iterables, chain provides an efficient and flexible way to handle such tasks, making it easier to process and manipulate sequences in your code.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare