Home > OS >  Can someone try to explain to me how this code works? (programmingwithmosh's)
Can someone try to explain to me how this code works? (programmingwithmosh's)

Time:09-17

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!

CodePudding user response:

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.

CodePudding user response:

The list newnum is only empty to begin with.

During each iteration of the for, it is added to (append).

New numbers are only added to it if they aren't present in it at that point in time (not in).

Hence its final value is the deduplicated list of numbers.

number = [1,2,3,4,5,5,6,6,7,8,9]
newnum = []
# For each number in the top list
for num in number:
    # If this number is not in the second list
    if num not in newnum:
        # Add it to the end of the second list
        newnum.append(num)
print(newnum)

Sidenote: an easier way to do this is using set, whose elements are guaranteed to be unique (if you add the same number twice, the set will still only contain one of that number).

newnum = list(set(number))
  • Related