🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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])
  1. A.['pen']
  2. B.[4, 2]
  3. C.['book', 'bag']
  4. 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 →