Embercode
✕
Built-in CollectionsQuick quiz

Iterators and changing a list while looping

6 questions. Covers Changing a collection during a loop in Python.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``python xs = ['a', 'b', 'c', 'd', 'e'] for s in xs: if s == 'd': xs.remove(s) print(xs) ``
  2. 2. What does this program print? ``python nums = [10, 15, 20, 25, 30] i = 0 while i < len(nums): if nums[i] % 10 == 0: del nums[i] else: i += 1 print(nums) ``
  3. 3. The loop below is meant to remove every 'Ben' from the list. What is the bug? ``python names = ['Ana', 'Ben', 'Ben', 'Dev'] for n in names: if n == 'Ben': names.remove(n) print(names) ``
  4. 4. This loop is meant to remove the even numbers from a list. What happens when it runs? ``python v = [1, 2, 3, 4, 5] for i in range(len(v)): if v[i] % 2 == 0: del v[i] print(v) ``
  5. 5. The loop below removes people younger than 30. Where is the error raised, when the program fails? ``python ages = {'Ana': 31, 'Ben': 25} for name in ages: if ages[name] < 30: del ages[name] ``

5 question(s) left on this page.