Dictionaries · Lookup By Value · Practice Question
Lookup by even values
Question
Given the following dictionary:
dict:`AAPL`MSFT`GOOG`TSLA`NVDA!101 200 305 400 502Extract the keys whose corresponding values are even numbers.
Solution
where not dict mod 2Result:
`MSFT`TSLA`NVDAAlternative Solution
(key dict) where 0 = (value dict) mod 2Result:
`MSFT`TSLA`NVDAExplanation
dict mod 2 applies mod key-wise, giving 1 for odd and 0 for even values. not flips this to a boolean dictionary where 1b marks the even entries, and where returns those keys. The alternative does the same thing on value dict/key dict directly rather than operating on the dictionary itself — the first solution is more idiomatic.