Skip to content

Instantly share code, notes, and snippets.

@raeq
Last active May 8, 2020 19:31
Show Gist options
  • Save raeq/cf3aa312343cca9b5c9bbbc0913bf3b2 to your computer and use it in GitHub Desktop.
Save raeq/cf3aa312343cca9b5c9bbbc0913bf3b2 to your computer and use it in GitHub Desktop.
"""
Zipping two lists to a dictionary as an example of dict comprehension.
"""
import typing
def dict_from_list(keys: [typing.Hashable], values: [])-> typing.Dict:
"""
Zips two lists. Returns: dict
"""
if len(keys) != len(values):
raise ValueError("Sequences are different lengths.")
return dict(zip(keys, values))
countries: list = ["China", "India", "United States", "Indonesia", "Pakistan"]
population_m: list = [1400, 1360, 330, 270, 221]
print(dict_from_list(countries, population_m))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment