Quick answer: The idiomatic Python swap is tuple assignment: a, b = b, a. Python evaluates the right-hand side before assigning the targets, so neither value is lost. Remember that swapping names changes references; it does not copy or clone mutable objects.

Swapping values in Python usually means exchanging what two names, list positions, or dictionary keys refer to. The idiomatic way to swap two names is tuple assignment: a, b = b, a. It is short, readable, and does not need a temporary name.
The official Python documentation covers lists, tuples and sequences, and assignment statements.
Python evaluates the right side first, then assigns the results to the left side. That is why a, b = b, a works without losing either original value.
Use the tuple-assignment form for normal Python code. Use a temporary name only when teaching the general concept or when translating code from another language that requires it.
Swapping does not copy large objects by value. It changes which objects the names or containers refer to. That makes the operation fast, but it also means mutable objects still behave like normal shared Python objects after the swap.
When swapping inside a container, remember that the container itself changes. That is different from swapping two local names, which only changes those names.
Swap Two Names
The direct Python swap uses tuple assignment.
a = 10
b = 20
a, b = b, a
print(a)
print(b)
The output is 20 and 10. Python reads the old values of b and a before either name is changed.
This is the clearest default for exchanging two names in Python.
The same syntax works for many object types because Python is assigning references, not performing numeric arithmetic.
Swap With A Temporary Name
A temporary name is more verbose, but it shows the general algorithm used in many languages.
left = "red"
right = "blue"
temporary = left
left = right
right = temporary
print(left)
print(right)
This produces the same result as tuple assignment. It stores one original value before overwriting the name.
Use this only when the step-by-step logic needs to be shown. In normal Python code, tuple assignment is easier to scan.
A temporary name is still useful when the swap is part of a longer explanation or when each step needs a breakpoint during debugging.

Swap List Items
Tuple assignment also works with list indexes.
items = ["first", "second", "third"]
items[0], items[2] = items[2], items[0]
print(items)
This swaps the first and last items in place. The list object is changed, so any other reference to the same list sees the updated order.
Check that indexes are valid when they come from user input or another data source. An invalid index raises IndexError.
This pattern is common in sorting algorithms, shuffling logic, and small rearrangement tasks. It updates the existing list rather than creating a new one.
Swap Dictionary Values
You can swap values stored under two dictionary keys.
scores = {"Ada": 91, "Grace": 88}
scores["Ada"], scores["Grace"] = scores["Grace"], scores["Ada"]
print(scores)
This keeps the keys the same and exchanges their values. It is useful when the dictionary structure should remain intact.
If a key might be missing, check membership first or use a try/except block that raises a clear error for the caller.
Dictionary value swaps are useful when the keys identify fixed positions or names but the values need to trade places.

Swap Three Values
Tuple assignment can rotate more than two names at once.
a = "A"
b = "B"
c = "C"
a, b, c = c, a, b
print(a)
print(b)
print(c)
The right side is evaluated first, so the original values are rotated safely. This can be useful for small state changes, but do not overuse it when a longer function would be clearer.
For complex rearrangements, write intermediate names or comments so future readers can confirm the intended mapping.
If the rotation represents a real domain operation, wrap it in a function with a clear name instead of leaving a dense assignment in the middle of other logic.
Avoid Arithmetic Swap Tricks
Some languages show arithmetic swaps such as adding and subtracting numbers. Python does not need those tricks.
x = 7
y = 4
x, y = y, x
print(x + y)
print(x, y)
Tuple assignment works for numbers, strings, objects, lists, and most other values. Arithmetic tricks only work for numeric values and are easier to get wrong.
Arithmetic swaps can also overflow in fixed-width languages. Python integers avoid that specific overflow issue, but the trick is still less readable than the direct assignment form.
The practical rule is simple: use a, b = b, a for two names, the same pattern for list indexes or dictionary keys, and a temporary name only when it improves explanation.
Good tests should include swapping equal values, swapping list endpoints, swapping dictionary values, and confirming that a list swap mutates the original list object.

Use Tuple Assignment
The right side is evaluated first and packed for assignment, then the targets receive the old values in the new order. This is concise, readable, and works for numbers, strings, objects, and many other values.
Swap Container Positions
For list elements, assign indexes together after validating that the indexes are distinct and in range. This mutates the list, unlike swapping two local names, which only changes name bindings.

Understand Object References
Python names refer to objects. A swap does not make copies, so two names can still refer to mutable objects whose contents change later through either reference.
Know When A Temporary Helps
A temporary variable can make a multi-step mutation easier to teach or debug, and it may be clearer when the swap is part of a larger transaction. It is not required for the ordinary two-name case.
Test The Invariant
Test equal values, different types, mutable objects, list indexes, dictionary values, and a multi-target assignment. Verify both the targets and the container or object state after the operation.
The Python assignment reference explains evaluation and target binding. Related references include expressions, dynamic attributes, and tests.
For related object updates, compare dynamic attributes, expressions, and invariant tests when changing bindings or containers.
Frequently Asked Questions
How do I swap two variables in Python?
Use tuple assignment: a, b = b, a. Python evaluates the right side before assigning the new values.
Do I need a temporary variable?
Usually no. A temporary name can help teach the concept or make a multi-step mutation explicit.
Can I swap list elements?
Yes. Assign the two indexed positions together, but validate the indexes and remember that the list itself is mutated.
Does swapping copy objects?
No. Names and containers are rebound to existing objects; mutable objects still have their normal shared-reference behavior.