-1

searching for a solution, but can't find any. I need to create a program that prompts the user for a list of integers , stores in another list only unique integers (for example 1 to 10), and displays the resulting list. For this moment I have a program that just copies full list and prints it without sorting integers:

def inputnumber():
    numbers = input('Enter number(s): ').split(',')
    return [int(i) for i in numbers]

x = inputnumber()
y = x[:]
print(y)

and sorry guys, I'm just a beginner and just trying out everything. Should I work with del function theres another way?

Dinozauras
  • 17
  • 1
  • 6
  • 1
    Convert to a [set](https://docs.python.org/2/library/stdtypes.html#set). Sets only hold unique values. – khelwood Dec 13 '14 at 20:39
  • theres alway a knee-jerks that rate quastion negative or I'm asking wrong? I'm just started to learn about lists, sequences and tuples. Never heard of set function – Dinozauras Dec 13 '14 at 20:42
  • 1
    @Dinozauras How about you take a few moments [to learn them](https://docs.python.org/2/tutorial/datastructures.html#sets)? Sets are fun! And so is learning about new constructs in a programming language that help you solve your problems. – Stefan van den Akker Dec 13 '14 at 21:03

3 Answers3

1

Note that if you use only set, you will not get a list but another kind of type, so you have to make it:

MyList = list(set(something))
Abdelouahab
  • 7,331
  • 11
  • 52
  • 82
0

You could try converting the list to a set.

def inputnumber():
    numbers = input('Enter number(s): ').split(',')
    return [int(i) for i in numbers]

x = inputnumber()
y = set(x)
print(y)

As mentioned by @khelwood, sets only contain unique values, which means all duplicates will be removed.

Also keep in mind that sets are not ordered, so the numbers might be shown in a mixed order.

Simon Farshid
  • 2,636
  • 1
  • 22
  • 31
  • now I'm totally confused since i've never heard of this set thing, still not sure how I would get unique values (has it something to do with s.remove or s.discard functions?) – Dinozauras Dec 13 '14 at 20:59
  • A list is a collection of items (numbers in this case) and it saves the order of these items. If you add 1, 2 and 3; you will get 1 2 3 in the same order. A set however, only contains unique items. If you try to add duplicates, it just discards them. The downside is that sets don't remember the order the items were added to them. – Simon Farshid Dec 13 '14 at 21:01
  • so if the person would type when asked 1, 11, 22, 6, 5 (when I run program), I need program to give me a result of [1, 6, 5]. Does set really the key? If so, I would start searching the right answer – Dinozauras Dec 13 '14 at 21:09
0

you can use set to remove duplication:

def inputnumber():
    numbers = input('Enter number(s): ').split(',')
    return set([int(i) for i in numbers])
Hackaholic
  • 19,069
  • 5
  • 54
  • 72