← Quiz: Python Dictionaries: all 15 questions
Quiz: Python Dictionaries · Question 15 of 15
What does this print? (code)
What does this print?
stock = {"pen": 0, "book": 4, "bag": 2}
print([item for item, qty in stock.items() if qty > 0])
- A.['pen']
- B.[4, 2]
- C.['book', 'bag']
- D.['pen', 'book', 'bag']
Answer & explanation
Correct answer: ['book', 'bag']
items() gives (key, value) pairs, and only items with a quantity above 0 are kept.
Can you get the rest right without peeking?
Take the timed Quiz: Python Dictionaries quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →