w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Switching Two Values in Dictionary, Keeping Keys Constant

Use tuple assignment:

d['bar'], d['foo'] = d['foo'], d['bar']

This simply swaps the values. The Python compiler optimizes for such cases, and this doesn't require any frame stack pushes (provided d doesn't implement __getitem__ and / or __setitem__ hooks in Python code).





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.