So basically, I don’t really get how this simple code makes sense. The purpose is to identify the duplicates in a list and remove them. The code is:
number = [1,2,3,4,5,5,6,6,7,8,9]
newnum = []
for num in number:
if num not in newnum:
newnum.append(num)
print(newnum)
Specifically in the if num not in newnum, why does it need to compare to an empty list instead of the original list? How does that work and how does it know that the numbers in the number list is not in the newnum list? When I first thought of a way of solving this problem, it was pretty much the same but I would’ve put a != operator on it but (Hopefully that’s possible too) I gave up. If anyone’s willing to try explaining this simple question thank you very much!
>Solution :
Let’s change the original sequence a bit (to make it more interesting), and add a print(), the most venerable of debugging tools to see how newnum evolves during the loop:
number = [1,2,3,4,6,5,5,6,6,7,8,9]
newnum = []
for i, num in enumerate(number):
print(f"{i=}, {num=}, {newnum=}")
if num not in newnum:
newnum.append(num)
print(newnum)
The output is (highlights mine)
i=0, num=1, newnum=[]
i=1, num=2, newnum=[1]
i=2, num=3, newnum=[1, 2]
i=3, num=4, newnum=[1, 2, 3]
i=4, num=6, newnum=[1, 2, 3, 4]
i=5, num=5, newnum=[1, 2, 3, 4, 6] # 6 was added to the list
i=6, num=5, newnum=[1, 2, 3, 4, 6, 5] # 5 was added to the list
i=7, num=6, newnum=[1, 2, 3, 4, 6, 5] # neither 6 or 5 is added, they're there
i=8, num=6, newnum=[1, 2, 3, 4, 6, 5] # neither 6 or 5 is added, they're there
i=9, num=7, newnum=[1, 2, 3, 4, 6, 5] # neither 6 or 5 is added, they're there
i=10, num=8, newnum=[1, 2, 3, 4, 6, 5, 7]
i=11, num=9, newnum=[1, 2, 3, 4, 6, 5, 7, 8]
[1, 2, 3, 4, 6, 5, 7, 8, 9]
As you can see, newnum is only empty at the very beginning of the loop, and as numbers that aren’t already there (num not in newnum) are encountered, they’re appended to it. If a number already is in the newnum list, it’s not appended again.