How can I use “not in” to remove duplicates from a list?
Answer
When we use not in, it results in Trueonly if the object on the left is not present in the object on the right. For example, if we have a list of numbers and want to know if the number 10 is present in it, and print a message to the user if it is not, we could write:
if 10 not in my_list:
print "10 not found!"
To expand on that, we can check for duplicates by creating an empty list to put our non-duplicate numbers into. Then, inside of a loop that goes through our input list, we’d do a check similar to the code above and look for the current loop variable value to not be in our output list. If it isn’t, we append() that value to it!
Then be sure to return that output list outside of the loop, otherwise your code will stop early!
The answer does even use the a not in b syntax suggested in the hint.
I came to your first solution by myself as well, but indeed it throws the error: “TypeError: argument of type ‘NoneType’ is not iterable”
est is an empty list, to start out. Examine the inner for loop. On its first run, what will c be? You got it, None.
Now we see why the above error was thrown. We cannot use not in None since in expects an iterable.
est, on the other hand is iterable, even if empty. Now not in can be executed and will yield True if i is NOT in the list, or False otherwise. When true, append i to the list. The resulting list will have no duplicates.
def remove_duplicates(list):
z = []
for x in range(len(list)):
if list[x] not in z:
z.append(list[x])
print z
return z
y = [1, 2, 3, 4, 4]
remove_duplicates(y)
I didn’t really check the solution, but I’m curious about how my code would fare compared to it:
def remove_duplicates(le):
copy_list = le[:]
new_list = []
duplicates_list = []
a = 0
b = 0
c = 0
for a in range(0,len(le)):
for b in range(a+1,len(le)):
if le[a] == le[b]:
duplicates_list.append(le[b])
if len(duplicates_list) > 1:
for c in range(0,len(duplicates_list)-1):
if duplicates_list[c] == duplicates_list[c+1]:
duplicates_list.pop(c+1)
for item in copy_list:
if item not in duplicates_list:
new_list.append(item)
for item in duplicates_list:
new_list.append(item)
return new_list
It just feels like I wrote so much code to do so little in a language where everything seems to be done in few lines
Would someone more experienced be able to tell me where am I wasting time on these types of code? Thanks in advance
I am just curious as to why this is how codecademy did the assignment. The code listed below is what shows up.
def remove_duplicates(inputlist):
if inputlist == []:
return []
# Sort the input list from low to high
inputlist = sorted(inputlist)
# Initialize the output list, and give it the first value of the now-sorted input list
outputlist = [inputlist[0]]
# Go through the values of the sorted list and append to the output list
# ...any values that are greater than the last value of the output list
for i in inputlist:
if i > outputlist[-1]:
outputlist.append(i)
return outputlist
Is this a more efficient way of doing it? My code works perfectly and is listed below.
def remove_duplicates(lst):
result = []
for i in lst:
if i not in result:
result.append(i)
return result
n = [1, 1, 2, 2, 3, 3, 4, 4]
print remove_duplicates(n)
Two things remain unchanged in your code (which is a good thing)…
The order of the original list is preserved.
The order of the output list matches the input list.
The author of the solution might think it clever to sort and compare the last element to the item in question, but the outcome is a sorted list, and the original list is reordered.
Your indentation seems a little funny to me. Even on codeacademy the standard is two whitespace characters for code blocks and four is the standard for many styles. A single whitespace is asking for trouble. As for the issue, remember what return does in a function. When a return statement is executed the entire function ends.