Iterating over Python Lists
Posted: Thu Oct 27, 2016 9:20 pm
What will the following Python program do?
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?
- """Remove al 2's from the list"""
- x = [2,2,2,2,2,5,6,7,8,9,2,2,2]
- def Remove_2():
- for number in x:
- if number==2:
- x.remove(number)
- return x
- print ("The list without 2's:", Remove_2())