Skip to content

Instantly share code, notes, and snippets.

@zhum
Created September 10, 2025 22:22
Show Gist options
  • Select an option

  • Save zhum/27c3bb204f96c6787d054fbf1722eb24 to your computer and use it in GitHub Desktop.

Select an option

Save zhum/27c3bb204f96c6787d054fbf1722eb24 to your computer and use it in GitHub Desktop.
Simple python tricks
# 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