Removing duplicate elements in a python list

Asked

Viewed 42,162 times

4

I have to do a function and I’m not being able to develop the logic. Here’s the thing:

Write the remove_repeated function that takes as a parameter a list of integer numbers, checks whether such a list has repeated elements and removes them. The function should return a list corresponding to the first list, without repeating elements. The returned list should be sorted.

Tip: You can use lista.sort() or sorted(lista).

  • friend this was a challenge of USP in Python correct? try to try harder and ask questions related to your code...

  • 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".

6 answers

10

A function pythonica to solve this problem would:

def remove_repetidos(lista):
    l = []
    for i in lista:
        if i not in l:
            l.append(i)
    l.sort()
    return l

lista = [1, 1, 2, 1, 3, 4, 3, 6, 7, 6, 7, 8, 10 ,9]

lista = remove_repetidos(lista)
print (lista)

Exit:

>>>[1, 2, 3, 4, 6, 7, 8, 9, 10]

It is very easy to understand how the function works, so I will omit more explanations.

  • I came across this challenge from USP today and I wasn’t able to do it, thank you. I was trying to use the del to delete the repeated elements of the list, but this messed up the indexes, causing the for did not work properly.

7

The list must be ordered, but in the same order as the original or can be ordered later?

I say that because set() is used precisely for this and is very efficient. In general you should avoid reprogramming what is already in the standard library.

>>> lista = [1, 1, 2, 1, 3, 4, 3, 6, 7, 6, 7, 8, 10 ,9]
>>> sorted(set(lista))
[1, 2, 3, 4, 6, 7, 8, 9, 10]

Already if you need to keep the same order from the original list see that answer: https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-whilst-preserving-order

Or create a Orderedset which would be the equivalent of a Collections.Ordereddict only for lists. It works well too.

1

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]

0

I think you can do using the function del, as follows:

def remove_repetidos(l):

    lista = l
    i = 0
    while i < len(lista):
        j = i + 1
        while j < len(lista):
            if lista[j] == lista[i]:
                del(lista[j])
            else:
                j = j + 1
        i = i + 1

    return sorted(lista)```

0

had problems using lista.sort() or sorted(lista), (if it’s a txt list, you put a " n" every time you hit "enter", then I did it another way)

Code:

def tira_nome(lista):
    listaP = []
    num = 0
    for lis in lista:
        lis = lis.rstrip()
        for letter in listaP:  
            if letter == lis:
                num = 1
        if num == 0:
            listaP.append(lis)
            num = 0
        else:
            num = 0
    return listaP

-1

numeros = input("Digite uma lista com números inteiros: ")


print("Você digitou os seguintes números: ")
print(numeros)

#Cria a função remove_repetidos que cria e ordena um set a #partir de uma lista
remove_repetidos = lambda l: list(sorted(set(l)))

'''remove_repetidos.remove(" ")'''

print("Sua lista filtrada e ordenada: ")
print(remove_repetidos(numeros))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.