0
a_list=[1,1,1,1,3,4,5]
one_list=[]
for a in a_list:
    if a == 1:
        one_list.append(a)
        a_list.remove(a)

print(a_list)
print(one_list)

i need to remove 1 from old list which is a_list and append it in a new list, one_list. this is how i did it but the output is not like i expected it to be.

here is what i got

[1, 1, 3, 4, 5]
[1, 1]

but my expected output would be

[3, 4, 5]
[1, 1, 1, 1]
Stack Vau
  • 41
  • 4
  • 1
    Don't remove an item in a `for` loop. Does this answer your question? [How to remove list elements in a for loop in Python?](https://stackoverflow.com/questions/10665591/how-to-remove-list-elements-in-a-for-loop-in-python) – j1-lee Nov 05 '21 at 03:57
  • Removing items from something you're iterating over is a recipe for headaches. – ddejohn Nov 05 '21 at 03:57

0 Answers0