You cannot use tuples to do the same as you did with the list because the tuples are immutable and thus cannot add new values. To add a new value in the tuple it is necessary to create a new tuple with the current values plus the new one and this addition is made from an auxiliary structure.
For example, it is possible to do all the logic using lists and just return as tuple:
def remove_repetidos(lst):
lista = [] #lista vazia
for i in lst:
if i not in lista:
lista.append(i)
lista.sort() #ordenada a lista por ordem ascendente
return tuple(lista)
And to create a new tuple by adding the new value would look something like:
def remove_repetidos(lst):
unicos = tuple()
for valor in lst:
if valor not in unicos:
unicos += (valor,)
return unicos
But note the fact that even though it looks like it’s only modifying the tuple you’re actually creating a new tuple and overwriting the old one.
The simplest way to remove duplicate items from a sequence is by using the set
, as can be seen in Remove Repeated Integers List in Python
At what point exactly would you like to apply the tuples?
– Woss
Meanwhile I made another one that’s not working and I’m not sure why.
– PipocaInsuflada
def sensas(lst): lst_unica = [i for n, i in enumerate(lst_com_unicos) if i not in lst_com_unicos[n + 1:]] for lst_resulta_unica in lst_unica: Return lst_resulta_unica
– PipocaInsuflada
This other code seems to be wrong. You use a variable
lst_com_unicos
that has not been defined.– Woss
sorry I’m new in python, I thought he assumed. I’ll redo, I put here
– PipocaInsuflada
def sensas(lst): lst_com_unicos = () lst_unica = [i for n, i in enumerate(lst_com_unicos) if i not in lst_com_unicos[n + 1:]] for lst_resulta_unica in lst_unica: Relstturn_resulta_unica
– PipocaInsuflada