Created
June 3, 2023 17:00
-
-
Save eder-projetos-dev/aa8a568d1e1321cd42616ae64d718925 to your computer and use it in GitHub Desktop.
Python - Dictionary Comprehensions
This file contains 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
names = ['Alice', 'Bob', 'Charlie'] | |
ages = [25, 32, 40] | |
people = {name: age for name, age in zip(names, ages)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dictionary Comprehensions
I particularly like dictionary comprehensions when I need to create dictionaries based on existing data. It provides a concise and readable way to build dictionaries with key-value pairs. For example, I can use a dictionary comprehension to create a dictionary mapping names to ages using lists of names and ages.
https://levelup.gitconnected.com/11-tricks-that-will-make-your-life-easier-as-a-python-developer-da29e4306675