0

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!

3
  • It doesn't "compare to an empty list", it looks at whether the number is in the second list, and if it's not, it's added, so it's no longer empty. Commented Sep 16, 2022 at 14:17
  • And no, using != would be plain wrong (as a number never compares to a list). Commented Sep 16, 2022 at 14:18
  • Try printing all the values on each loop cycle, it might shed some light. Commented Sep 16, 2022 at 14:20

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

okay I've read this code for a few minutes now and I kind of get it now. I'd just like to confirm if my thoughts were right. in this line: if num not in newnum: newnum.append(num) I'm a beginner in coding and I thought that an if else code must meet a certain criteria before being executed but it seems like that specific code can run properly even if its criteria is not yet met?
Um... What criteria would you say isn't met?
Prob. it worth to mention this way (approach) to get all numbers in non-duplicated is not very efficient - when you are dealing with a huge list...
0

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)) 

1 Comment

okay this def helped too because I thought you had to put something in the empty list first before knowing (if not) if it's in there or not

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.