Skip to content

Instantly share code, notes, and snippets.

@raeq
Created August 1, 2020 18:25
Show Gist options
  • Save raeq/6e5443662e3afa732fbbe5f9d4439e4c to your computer and use it in GitHub Desktop.
Save raeq/6e5443662e3afa732fbbe5f9d4439e4c to your computer and use it in GitHub Desktop.
Return a dict from two lists
def dict_from_two_lists(keys: list, values: list) -> dict:
"""
Args:
keys: The list of keys for the dictionary
values: The list of values for the dictionary
Returns: A dictionary of key:value pairs
"""
if len(keys) != len(values):
raise ValueError("Lists must be of same length")
return dict(zip(keys, values))
assert dict_from_two_lists(["first", "second", "third"],
["primary", "secondary", "tertiary"]) == {
"first": "primary",
"second": "secondary",
"third": "tertiary",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment