You have two parallel lists: headers ["name", "age", "city"] and a row ["Alex", 30, "NYC"]. You want one dict {"name": "Alex", "age": 30, "city": "NYC"}. How do you pair them up?
Loop by index? for i in range(len(keys)): result[keys[i]] = values[i]?
That works, but Python has a cleaner pairing tool called zip. It takes two iterables and yields pairs of same-index elements:
zip(["name", "age"], ["Alex", 30])
# yields ("name", "Alex"), ("age", 30)And dict() accepts any iterable of pairs and turns them into key-value entries. Compose dict and zip and you have a one-liner.
So dict(zip(keys, values)) iterates both lists in lockstep and builds the dict in one call?
That's the whole move. Iterate in lockstep, pair each key with its value, wrap in a dict:
def zip_to_dict(keys: list, values: list) -> dict:
return dict(zip(keys, values))No index management, no off-by-one risk, no loop body.
What if the lists are different lengths — say 3 keys but 2 values?
zip stops at the shorter iterable silently. So dict(zip(["a", "b", "c"], [1, 2])) is {"a": 1, "b": 2} — the third key gets no pair and vanishes. No error. If strict length matching matters, compare len(keys) == len(values) first.
And this is basically what CSV parsers do internally — headers plus a row, zipped into a dict.
Exactly what csv.DictReader does under the hood. You just built a tiny version of it. Whenever you have two aligned lists that should become a dict — headers and values, names and scores, IDs and labels — reach for dict(zip(a, b)).
TL;DR: dict(zip(keys, values)) pairs two parallel lists into a dict in one expression.
zip(a, b) — yields (a[0], b[0]), (a[1], b[1]), etc.dict(pairs) — accepts any iterable of 2-tuples| keys | values | Result |
|---|---|---|
["a", "b", "c"] | [1, 2, 3] | {"a": 1, "b": 2, "c": 3} |
["a", "b", "c"] | [1, 2] | {"a": 1, "b": 2} |
["a"] | [1, 2, 3] | {"a": 1} |
For strict length matching, compare len() first. Otherwise zip degrades gracefully.
You have two parallel lists: headers ["name", "age", "city"] and a row ["Alex", 30, "NYC"]. You want one dict {"name": "Alex", "age": 30, "city": "NYC"}. How do you pair them up?
Loop by index? for i in range(len(keys)): result[keys[i]] = values[i]?
That works, but Python has a cleaner pairing tool called zip. It takes two iterables and yields pairs of same-index elements:
zip(["name", "age"], ["Alex", 30])
# yields ("name", "Alex"), ("age", 30)And dict() accepts any iterable of pairs and turns them into key-value entries. Compose dict and zip and you have a one-liner.
So dict(zip(keys, values)) iterates both lists in lockstep and builds the dict in one call?
That's the whole move. Iterate in lockstep, pair each key with its value, wrap in a dict:
def zip_to_dict(keys: list, values: list) -> dict:
return dict(zip(keys, values))No index management, no off-by-one risk, no loop body.
What if the lists are different lengths — say 3 keys but 2 values?
zip stops at the shorter iterable silently. So dict(zip(["a", "b", "c"], [1, 2])) is {"a": 1, "b": 2} — the third key gets no pair and vanishes. No error. If strict length matching matters, compare len(keys) == len(values) first.
And this is basically what CSV parsers do internally — headers plus a row, zipped into a dict.
Exactly what csv.DictReader does under the hood. You just built a tiny version of it. Whenever you have two aligned lists that should become a dict — headers and values, names and scores, IDs and labels — reach for dict(zip(a, b)).
TL;DR: dict(zip(keys, values)) pairs two parallel lists into a dict in one expression.
zip(a, b) — yields (a[0], b[0]), (a[1], b[1]), etc.dict(pairs) — accepts any iterable of 2-tuples| keys | values | Result |
|---|---|---|
["a", "b", "c"] | [1, 2, 3] | {"a": 1, "b": 2, "c": 3} |
["a", "b", "c"] | [1, 2] | {"a": 1, "b": 2} |
["a"] | [1, 2, 3] | {"a": 1} |
For strict length matching, compare len() first. Otherwise zip degrades gracefully.
Write `zip_to_dict(keys, values)` that returns a dict built by pairing each key with the value at the same index. Use `dict(zip(keys, values))`.
Tap each step for scaffolded hints.
No blank-editor panic.