Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Python: Swap Keys and Values in Dictionary

📅 2010-Jan-28 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ dictionaries, python ⬩ 📚 Archive

aDict = {7:“Seven”, 10:“Ten”, 2:“Two”} bDict = {val:key for (key, val) in aDict.items()} print(bDict) # {‘Seven’: 7, ‘Two’: 2, ‘Ten’: 10}


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧