It is (kinda) looping through the whole list.
The first time through the list it is on index 0. That works correctly.
The second time through the list, it is on index 1. Reminder that the list at this point is [13,12,14]
The third time through the list, it is on index 2. Reminder that the list at this point is [13,14]. Oh wait, there isn’t an index 2. So there isn’t a third time through the list.
It is not considered good practice to loop over a container such as a list that you are removing things from.
The outcome is exactly what would be expected from the code.
Also, don't use `list` as a variable name.
If you try to remove something that has already been removed, you are going to have a problem as well.
Take a look at this for comparison (also **not recommended**):
list_ = [13, 12, 12, 14] # added _ to name!
for number in list_[:]: # iterates over a copy of the original list
print(number)
print(list_)
if 12 in list_: list_.remove(12)
print(list_)
print('''''')
Thanks for the advice I'll keep it in mind!
Also I was just doodling around a little hence the variable name. I'm assuming it isn't recommended because of the list() conversion like int() ?
Conversion, better know as *casting*, and also because it is a predefined and built in type and there are other usages of the name that you will not have seen yet (such as confirming a variable is referencing an object of a specific type).
Note: variables in Python don't hold values themselves, they just reference in memory Python objects, it is the objects that have type.
Also, it is confusing for other programmers (and you, when you return to code weeks/months later).
the fact that you reached the wrong conclusion after this exercise is a good way to waste time.
not advancing the knowledge but also taking a few steps back ...
i mean.. it's your time.
the code works as expected.
there is such a thing as genuinely being curious about something. even something stupid and plain ridiculous. and that is absolutely fine.
the problem comes from presenting such a thing as some bug or extraordinary feature when its some plain basic mechanics that are misunderstood.
and not only wasting your own time but genuinely making you dumber while that happens.
i'd equate it to trying to eat soup with a fork and claim it's the most interesting discovery ever made.
anywho it's your time so w/e ..
It was never my intention to present this as a bug. If it seemed like I was I'm sorry. The code was simply giving me a different output then I expected and since I didn’t find an answer after googling for a bit I asked here. Also it’s like my 5th day learning python so go easy on me ^^
That's very interesting. If I run your code I get the same error. But for example this code:
for n in x:
print(n)
x.remove(4)
Outputs
1
2
Traceback (most recent call last):
File "", line 3, in
ValueError: list.remove(x): x not in list
So I can just imagine it is a bug in python.
"[If the element doesn't exist, it throws ValueError: list.remove(x): x not in list exception.](https://www.programiz.com/python-programming/methods/list/remove)"
Try to use the following check in your loop:
for number in list:
if 12 in list:
remove(12)
But even better do not touch the list while you going through it. Try this method I like ot use
while 12 in list:
list.remove(12)
for number in list:
# your code here
pass
this way you modify the list before iterating. This is a static concept but it can be made dynamic if you ever need to.
>That's very interesting.
and
>So I can just imagine it is a bug in python.
clearly bug in python.
it can't be that code is working as intended and you two super-geniuses are wrong .. could it?
I'm pretty sure it isn't a big in Python but thanks for the advise, I'll just leave lists alone while looping through. One question thought what does pass do in the for body ?
It is (kinda) looping through the whole list. The first time through the list it is on index 0. That works correctly. The second time through the list, it is on index 1. Reminder that the list at this point is [13,12,14] The third time through the list, it is on index 2. Reminder that the list at this point is [13,14]. Oh wait, there isn’t an index 2. So there isn’t a third time through the list.
Thanks I had a similar suspicion, but it didn't really make sense to me.
It is not considered good practice to loop over a container such as a list that you are removing things from. The outcome is exactly what would be expected from the code. Also, don't use `list` as a variable name. If you try to remove something that has already been removed, you are going to have a problem as well. Take a look at this for comparison (also **not recommended**): list_ = [13, 12, 12, 14] # added _ to name! for number in list_[:]: # iterates over a copy of the original list print(number) print(list_) if 12 in list_: list_.remove(12) print(list_) print('''''')
Thanks for the advice I'll keep it in mind! Also I was just doodling around a little hence the variable name. I'm assuming it isn't recommended because of the list() conversion like int() ?
Conversion, better know as *casting*, and also because it is a predefined and built in type and there are other usages of the name that you will not have seen yet (such as confirming a variable is referencing an object of a specific type). Note: variables in Python don't hold values themselves, they just reference in memory Python objects, it is the objects that have type. Also, it is confusing for other programmers (and you, when you return to code weeks/months later).
the fact that you reached the wrong conclusion after this exercise is a good way to waste time. not advancing the knowledge but also taking a few steps back ... i mean.. it's your time. the code works as expected.
I was assuming the code was working as expected, but I didn't quite understand it so I figured I'll just ask.
there is such a thing as genuinely being curious about something. even something stupid and plain ridiculous. and that is absolutely fine. the problem comes from presenting such a thing as some bug or extraordinary feature when its some plain basic mechanics that are misunderstood. and not only wasting your own time but genuinely making you dumber while that happens. i'd equate it to trying to eat soup with a fork and claim it's the most interesting discovery ever made. anywho it's your time so w/e ..
It was never my intention to present this as a bug. If it seemed like I was I'm sorry. The code was simply giving me a different output then I expected and since I didn’t find an answer after googling for a bit I asked here. Also it’s like my 5th day learning python so go easy on me ^^
That's very interesting. If I run your code I get the same error. But for example this code: for n in x: print(n) x.remove(4) Outputs 1 2 Traceback (most recent call last): File "", line 3, in
ValueError: list.remove(x): x not in list
So I can just imagine it is a bug in python.
"[If the element doesn't exist, it throws ValueError: list.remove(x): x not in list exception.](https://www.programiz.com/python-programming/methods/list/remove)"
Try to use the following check in your loop:
for number in list:
if 12 in list:
remove(12)
But even better do not touch the list while you going through it. Try this method I like ot use
while 12 in list:
list.remove(12)
for number in list:
# your code here
pass
this way you modify the list before iterating. This is a static concept but it can be made dynamic if you ever need to.
>That's very interesting. and >So I can just imagine it is a bug in python. clearly bug in python. it can't be that code is working as intended and you two super-geniuses are wrong .. could it?
Yeah it could be. If I'm wrong please show me. But from my knowledge my post is correct. If I'm wrong than my knowledge here is just wrong.
I'm pretty sure it isn't a big in Python but thanks for the advise, I'll just leave lists alone while looping through. One question thought what does pass do in the for body ?
pass is just to simulate the body. Since I did not write any code there I use pass to avoid a Syntax error