Skip to content

Collection Into Set

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: collection-into-set

Section titled “Sourcery refactoring id: collection-into-set”

Use set when checking membership of a collection of literals

if currency in ["EUR", "USD"]:
take_payment()
if currency in {"EUR", "USD"}:
take_payment()

When checking if a variable is one of a collection of literals it is both more natural and more performant to use a set rather than a list or tuple to define that collection. Note that this can only be done where Sourcery can ascertain that the value being checked is of a hashable type.