Skip to content
CodeKruPro

Changing a collection while looping over it

QuizMediumPython onlyAsked in: Accenture, Infosys, TCS

Iterating means looping over the items. Which statements are true? Pick all that apply.

  • Adding keys to a dict inside for k in d: raises RuntimeError
  • Removing items from a list inside for x in lst: raises RuntimeError
  • for x in lst[:]: loops over a copy, so removing from lst is safe
  • lst = [x for x in lst if keep(x)] is a clean way to filter

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in