# Zip to Record

> Two lists become one record.

Canonical URL: <https://datadriven.io/problems/zip_to_record>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given two equal-length lists keys and values, return a dict mapping each key to its value by position.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **parallel iteration** and **dict construction** from two aligned lists. Zipping headers with values is a fundamental CSV/ETL pattern.

---

### Break down the requirements

#### Step 1: Pair each key with its corresponding value by position

The first key maps to the first value, second to second, and so on.

#### Step 2: Build and return a dict

Each pair becomes a key-value entry in the result dict.

---

### The solution

**Positional pairing into a dict**

```python
def zip_to_record(keys: list, values: list) -> dict:
    result = {}
    for i in range(len(keys)):
        result[keys[i]] = values[i]
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the number of keys.
> 
> **Space:** O(n) for the result dict.

> **Interviewers Watch For**
>
> Mentioning `dict(zip(keys, values))` as the idiomatic one-liner. But implementing the loop manually shows you understand what zip does under the hood.

> **Common Pitfall**
>
> Not handling mismatched lengths. If keys and values have different lengths, the loop will crash with an IndexError. Using `min(len(keys), len(values))` or checking upfront is defensive.

---

## Common follow-up questions

- What if keys and values have different lengths? _(Tests truncating to the shorter list or raising an error.)_
- What if some keys are duplicates? _(Tests that the last value wins in a dict, or collecting into a list of values per key.)_
- How would you handle this with pandas? _(Tests `pd.DataFrame([values], columns=keys)` or `pd.Series(values, index=keys)`.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/zip_to_record)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.