Skip to content

Merge List Appends Into Extend

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: merge-list-appends-into-extend

Section titled “Sourcery refactoring id: merge-list-appends-into-extend”

Merge consecutive list appends into a single extend.

my_list.extend([1, 2])
my_list.append(3)
my_list.append(4)
my_list.extend((5, 6, 7))
my_list.append(8)
my_list.append(9)
my_list.extend([1, 2, 3, 4, 5, 6, 7, 8, 9])

When adding multiple items to a list, it is cleaner to combine consecutive calls to list.append or list.extend into a single list.extend when possible.