Page 1 of 1

Iterating over Python Lists

Posted: Thu Oct 27, 2016 9:20 pm
by Eli
What will the following Python program do?
  1. """Remove al 2's from the list"""
  2. x = [2,2,2,2,2,5,6,7,8,9,2,2,2]
  3. def Remove_2():
  4.     for number in x:
  5.         if number==2:
  6.             x.remove(number)
  7.     return x
  8. print ("The list without 2's:", Remove_2())

My intention is that the code will delete all 2's from the list x and return a new list without 2's, why the program will not work the way I think?