How do you build a dictionary from a list of tickers and a list of prices in one line?
Coding
Easy
A Python puzzle about turning two lists into a dictionary.
Published
November 16, 2026
NoteQuestion
You have two Python lists, tickers = ["AAPL", "MSFT", "NVDA"] and prices = [190.0, 410.0, 130.0], with example closing prices. Write one line that builds a dictionary price_of that maps each ticker to its price, so that price_of["MSFT"] gives 410.0.
Try it first, then open the solution.
TipSolution
The answer is one line:
price_of =dict(zip(tickers, prices))
The function zip pairs the two lists by position, and dict turns each pair into a key and its value:
Position
Key, from tickers
Value, from prices
0
“AAPL”
190.0
1
“MSFT”
410.0
2
“NVDA”
130.0
Three slips are common:
Line
The slip
{tickers: prices}
a list cannot be a key, so Python raises a TypeError
dict(tickers, prices)
dict accepts at most one positional argument, so two raise a TypeError
{t: p for t in tickers for p in prices}
the two for clauses run one inside the other, so every ticker ends with the last price, 130.0