Another interesting way to resolve this issue is to use the method Ordereddict library Collections. In this case we can assemble the following code:
from collections import OrderedDict
def remove_repetidos(li):
return sorted(OrderedDict((i, None) for i in li))
lista = [1, 1, 2, 1, 3, 4, 3, 6, 7, 6, 7, 8, 10, 9]
print(remove_repetidos(lista))
The result of this algorithm is:
[1, 2, 3, 4, 6, 7, 8, 9, 10]
We can obtain the same result using the lambda expression associated with the function map(), as listed in the code below:
lista = [1, 1, 2, 1, 3, 4, 3, 6, 7, 6, 7, 8, 10, 9]
unicos = sorted(map(lambda x: x[1], filter(lambda x: x[1] not in
lista[:x[0]], enumerate(lista))))
print(unicos)
With this algorithm we get the same result that is:
[1, 2, 3, 4, 6, 7, 8, 9, 10]
Another interesting way is to use the following algorithm:
def remove_repetidos(li):
return sorted(dict(zip(li, li)).keys())
lista = [1, 1, 2, 1, 3, 4, 3, 6, 7, 6, 7, 8, 10, 9]
print(remove_repetidos(lista))
Thus obtaining the same result:
[1, 2, 3, 4, 6, 7, 8, 9, 10]
Another valid way to answer this question may be:
lista = [1, 1, 2, 1, 3, 4, 3, 6, 7, 6, 7, 8, 10, 9]
r = []
[r.append(i) for i in lista if not r.count(i)]
print(sorted(r))
Resulting in the following output:
[1, 2, 3, 4, 6, 7, 8, 9, 10]
friend this was a challenge of USP in Python correct? try to try harder and ask questions related to your code...
– Dorathoto
This question is constantly asked in the community, even a simple google search would return posts related to everything I hope you have understood about the content and before redoing the question could have searched about in the field called "SEARCH".
– stack.cardoso