This rule raises an issue when sum() is called with an empty list ([]) as the start parameter to concatenate or flatten lists.

Why is this an issue?

Using sum(list_of_lists, []) to concatenate lists is a common anti-pattern in Python. Because each + concatenation creates a new list and copies all previous elements into it, the overall operation has quadratic time complexity O(n2) instead of the linear O(n) that proper alternatives achieve.

Additionally, sum() is designed for numeric addition, not list operations. Using it for lists is semantically unclear and may confuse other developers reading the code.

What is the potential impact?

How to fix it

Code examples

Noncompliant code example

list_of_lists = [[1, 2], [3, 4], [5, 6]]
result = sum(list_of_lists, [])  # Noncompliant

Compliant solution

import itertools

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

How does this work?

itertools.chain.from_iterable() creates an iterator that yields elements from each sub-list in sequence without creating intermediate list copies. This avoids the repeated copying that causes the quadratic behavior of sum() and results in linear O(n) time complexity.

Resources

Documentation