1

So the goal of this code is to remove duplicates from the input and then print out a list without the duplicates and I think I got it but I can't seem to remember how to take in input with spaces and none of the things I have looked up so far have been very helpful to my case. Here's my code.

def eliminateDuplicates(lst):
    strnumbers = str(lst)
    listnumbers = list(strnumbers.split())    
    newlist = []
    for number in listnumbers:
        if number not in newlist:
            newlist.append(number)
    return newlist

def main():
    numbers = int(input("Enter numbers separated by space"))
    print("The distinct numbers are: ", eliminateDuplicates(numbers)) 

main()
1
  • You've asked a Python question, but your code isn't indented properly. Whitespace is syntactically significant in Python, and without proper indentation, we can't tell what your code is doing and so can't help you. Please edit your question and fix the indentation. Thanks! Nov 1, 2013 at 3:13

2 Answers 2

1
strnumbers = str(lst)
listnumbers = list(strnumbers.split())  

I think this sillyness is the cause of your problems (, and [ characters are going into your numbers). Just iterate over the input lst.

You will also need to work at sending a proper list into your function, which means you will need to change this line:

numbers = int(input("Enter numbers separated by space"))

I will leave that bit up to you.

2
  • Haha yeah, it is a little silly. I was just getting frustrated cause it kept trying to get it to work. Thanks for the help. Nov 1, 2013 at 3:28
  • I seem to have gotten all of it to work except for the fact that I cannot get the list to convert into integers in order for me to print it without the "". Nov 1, 2013 at 4:14
1

you cannot do

int("1 2 3")

but you can do

[int(x) for x in "1 2 3".split()]

one of the errors is coming from your int(input()) ... however I think wim is right

1
  • I didn't give that to you, lol. I don't know why you got a -1. Nov 1, 2013 at 3:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.