Script that takes a list of integer numbers, sorts and removes duplicates

Asked

Viewed 492 times

-3

I’m taking a course in python and I’m trying to solve the following exercise:

Exercise 1 - Removing repeated elements

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). What’s the difference?

I posted my code and I got a zero grade, I don’t understand where I’m going wrong. Follow:

def remove_repetidos (lista):
    lista2 = set(lista)
    lista = list(lista2)
    lista.sort()
    print (lista)


lista = []

while True:
    x = int(input('Numero: '))
    if x == 0:
        break
    lista.append(x)

remove_repetidos(lista)
  • Tip: see here how to format the code in the posts, and click [Edit] to do it in your question.

  • Thanks buddy, I already saved here

  • In the statement he gives examples of how the input and output should be?

  • Possible duplicate of Call list in function in python 3

  • You are doing nothing with the result of the function remove_repetidos. You didn’t tell us what website you’re doing the exercise on, but you may have to print the result or define a specific function so that it recognizes the answers. Read the instructions.

1 answer

1


I believe it’s self-explanatory, the function set() removes duplicates and function sorted() ordains lexicographically an eternal:

def remove_repetidos(lista):
  return sorted(set(lista))
  • You killed the riddle, with a code of two lines kkkk thanks, I passed the test. Another learning!

  • If the answer has served you, please mark as correct, so help others with the same problem.

  • I’m new here friend, I haven’t adapted to this site, I think I marked as correct, in case I did wrong let me know

Browser other questions tagged

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