Skip to content

Swap Variable

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.

Swap variable values with tuple assignment

temp = a
a = b
b = temp
a, b = b, a

When trying to swap the values held by two variables, we may avoid using an extra temporary variable unnecessarily by employing Python’s tuple assignment. As a consequence of this refactoring, the code is not only more concise but also reflects its intention more clearly.