Python Forum
Noob Alert! Wrong result using loop and if statemnent
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noob Alert! Wrong result using loop and if statemnent
#1
Hi there! I'm trying to leave out a specific integer in a list and remove every other number

nr = 9
my_list = [0,5,6,7,6,9,9,1,9]

for i in my_list:
    if i is not nr:
        my_list.remove(i)
print(my_list)
However I get [5, 7, 9, 9, 9] instead of [9,9,9]
What am I doing wrong?
Reply
#2
If you modify an object while iterating over it, it can cause problems.

Either iterate over a copy of it, or copy the data you do need and don't copy the rest. Examples:

Iterate over a copy:
nr = 9
my_list = [0,5,6,7,6,9,9,1,9]
 
for i in my_list[:]:
    if i is not nr:
        my_list.remove(i)
print(my_list)
Pull out the data you want, and overwrite the old reference
nr = 9
my_list = [0,5,6,7,6,9,9,1,9]
 
my_list = [x for x in my_list if x == nr]
print(my_list)
GJG likes this post
Reply
#3
Great! thanks bowlofred!
Reply
#4
As bowlofred mentions, the two examples not only solve the problem in different ways, but produce different results. The difference may be an unimportant side-effect, or it might break your code.
nr = 9
numbers = [0,5,6,7,6,9,9,1,9]

# This createa a new list
nines = [x for x in numbers if x == nr]
print('New List', nines, numbers)

# This code modifies the list
nines = numbers
for x in nines[:]:
    if x != nr:
        nines.remove(x)
print('Modifies', nines, numbers)
Output:
New List [9, 9, 9] [0, 5, 6, 7, 6, 9, 9, 1, 9] Modifies [9, 9, 9] [9, 9, 9]
Notice that the list comprehension does not change the original list. The remove.() for loop looks clunky, but it does modify the original list. All references to the list will see the changes.
GJG likes this post
Reply
#5
Thanks deanhystad!
By the way, in the block that modifies a list, you added:
nines = numbers
for x in nines[:]:
rather than going straight to
for x in numbers[:]
Is there any particular reason for it?
Sorry for all the noobish questions :)
Reply
#6
(Dec-19-2020, 08:31 AM)GJG Wrote: Is there any particular reason for it?
I guess he did it in order to preserve the original list and be able to show the original unmodified list to you afterwards.

Ops, that was stupid comment on my side. It shows that nines and numbers are actually the same [mutable] object and when modify mutable objects you need to be careful. nines = numbers does not creaty a copy of numbers, it just binds name nines to same object as numbers
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
So basically there's no point for it..?
Reply
#8
(Dec-19-2020, 04:55 PM)GJG Wrote: So basically there's no point for it..?
yes
nr = 9
numbers = [0,5,6,7,6,9,9,1,9]
for x in numbers[:]:
    if x != nr:
        numbers.remove(x)
print(numbers)
Output:
[9, 9, 9]
by the way - you can try it yourself. don't hesitate to experiment and play with the code.
GJG likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [ESP32 Micropython]Total noob here, I don't understand why this while loop won't run. wh33t 9 1,636 Feb-28-2023, 07:00 PM
Last Post: buran
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,388 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,320 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,877 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Running A Loop Until You See A Particular Result knight2000 6 31,611 Sep-04-2021, 08:55 AM
Last Post: knight2000
  python gives wrong string length and wrong character thienson30 2 2,945 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  List comprehensions-Wrong result RavCOder 4 2,227 Oct-08-2019, 10:16 AM
Last Post: RavCOder
  What Does This Part of Close Alert Code Mean? digitalmatic7 2 2,551 Feb-13-2018, 03:48 AM
Last Post: digitalmatic7
  boolean result of loop Skaperen 19 11,012 Apr-03-2017, 12:55 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020