Modifying Python Lists During Iteration

Think you can't modify a #Python list while iterating? You can append to it: mylist = [10, 20, 30] for i in mylist: print(i) if i < 100: mylist.append(i ** 2) This prints 10, 20, 30, 100, 400, and 900. BUT don't insert or remove. That'll cause real trouble.

  • No alternative text description for this image

There are many things you can do, that you shouldn't.

See more comments

To view or add a comment, sign in

Explore content categories