Created
August 2, 2023 14:32
-
-
Save e-dreyer/19f0b5470b96d73a36180be4e24bb1d9 to your computer and use it in GitHub Desktop.
Python zip usage with dictionaries
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
# Sample dictionaries | |
names = {'Alice': 25, 'Bob': 30, 'Charlie': 22} | |
cities = {'Alice': 'New York', 'Bob': 'San Francisco', 'Charlie': 'London'} | |
# Using zip to combine keys and values from both dictionaries | |
combined_data = zip(names.keys(), names.values(), cities.values()) | |
# Converting the zip object to a dictionary | |
result_dict = {name: (age, city) for name, age, city in combined_data} | |
print(result_dict) | |
# Output | |
{'Alice': (25, 'New York'), 'Bob': (30, 'San Francisco'), 'Charlie': (22, 'London')} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment