Created
September 10, 2025 22:22
-
-
Save zhum/27c3bb204f96c6787d054fbf1722eb24 to your computer and use it in GitHub Desktop.
Simple python tricks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # List to dict | |
| a = [10, 20, 30] | |
| b = {index: value for index, value in enumerate(a)} | |
| # {0: 10, 1: 20, 2: 30} | |
| a = ["a", 1, "b", 2, "c", 3] | |
| b = {a[i]: a[i + 1] for i in range(0, len(a), 2)} | |
| # {"a": 1, "b": 2, "c": 3} | |
| a = ["apple", "banana", "cherry"] | |
| b = {value: len(value) for value in a} | |
| # {'apple': 5, 'banana': 6, 'cherry': 6} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment