Iterating over Python Lists

Including Cython, Jython, IronPython, PyPy, Django framework, and interpreters: Ipython, IPython Jupyter/Notebook, CPython


Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5224
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

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?
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Python Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests