-2
Write a method rem() that accepts as input a list possibly containing duplicate values, and return a copy of the list where a copy of each duplicate value is removed. What I did:
def rem(lista):
i = 0
while i <= len(lista):
# print(lista[i])
if lista.count(i) >= 2:
lista.remove(i)
i += 1
print(lista)
lista = [3,4,5,6,5,5]
#lista = [4,4]
rem(lista)
Output example:
rem([2, 4, 2, 4, 4])
[2, 4, 4]
The code works for the list [3,4,5,6,5,5]
but not to [4,4]
.
What I did wrong?
Your solution does not work as you only have to remove a duplicate copy. The output of (rem(list)) should be [3,4,6,5,5]
– Ed S
You want to remove only once each duplicate value?
– afonso
that! Only one copy should be removed!
– Ed S
But at any specific position? You must remove the first duplicate or it may be the last?
– afonso
could be anyone!
– Ed S
I don’t understand why they denied the question! I tried to do!
– Ed S
Okay, I didn’t realize it at first. Now look at the correction.
– afonso
I didn’t understand some things in the code. Could you please comment?
– Ed S
Sure. What don’t you understand?
– afonso
For example: Seen = set([])
– Ed S
This initializes the variable that will store the list elements as they are 'seen'. The type
set
is a type of object that does not allow duplicates.– afonso
Based on your answer, I tried to make a new code. What do you think of it?
– Ed S