+ 1

Why does this code return None?

https://code.sololearn.com/c46sS0hXyO4d/?ref=app Hi all, I just have a question I'm trying to wrap my head around. It is definite a newbie question, so please bear with me. I've defined two functions in python to remove duplicates in a list: one using a for loop and another attempting to use list comprehension. My first function works like a charm, but I can't figure out what's wrong with the second one since it returns a list full of None, but the list length matches what it should be without duplicates.

14th May 2018, 10:43 PM
Bryan Moreno
Bryan Moreno - avatar
2 Answers
+ 2
You are putting the result of `temp.append(i)` into your list, not `i` itself. `temp.append(i)` returns None, and so that's that! You probably want to `return temp` at the end because that's where all the values are. Also, a personal preference thing: Building up a temp-list while doing a comprehension is a bit of an antipattern. List comprehensions are taken from maths and so they should be able to "exist on their own", and not incur any side effects (like manipulating outside variables). It's a minor complaint though really.
14th May 2018, 11:06 PM
Schindlabua
Schindlabua - avatar
0
try to compare the variables as it enters the loop
15th May 2018, 6:25 PM
Rajeeb