Back to Lookup By Value

Dictionaries · Lookup By Value · Practice Question

Lookup by even values

Sign in to track your progress

Question

Given the following dictionary:

dict:`AAPL`MSFT`GOOG`TSLA`NVDA!101 200 305 400 502

Extract the keys whose corresponding values are even numbers.

Solution

where not dict mod 2

Result:

`MSFT`TSLA`NVDA

Alternative Solution

(key dict) where 0 = (value dict) mod 2

Result:

`MSFT`TSLA`NVDA

Explanation

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.