Skip to content

Instantly share code, notes, and snippets.

@e-dreyer
Created August 2, 2023 14:32
Show Gist options
  • Save e-dreyer/19f0b5470b96d73a36180be4e24bb1d9 to your computer and use it in GitHub Desktop.
Save e-dreyer/19f0b5470b96d73a36180be4e24bb1d9 to your computer and use it in GitHub Desktop.
Python zip usage with dictionaries
# 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