1

Given a list of n numbers how can i print each element except the duplicates?

d = [1,2,3,4,1,2,5,6,7,4]

For example from this list i want to print : 1,2,3,4,5,6,7

Martin Spasov
  • 315
  • 3
  • 7
  • 23

3 Answers3

4

Since order doesn't matter, you can simply do:

>>> print list(set(d))
[1, 2, 3, 4, 5, 6, 7]

It would be helpful to read about sets

sshashank124
  • 31,495
  • 9
  • 67
  • 76
3

If the order does not matter:

print set(d)

If the type matters (want a list?)

print list(set(d))

If the order matters:

def unique(d):
    d0 = set()
    for i in d:
        if not i in d0:
             yield i
        d0.add(i)

print unique(d)
cadrian
  • 7,332
  • 2
  • 33
  • 42
0

All you have to do is

  1. create an array.
  2. get list's element.
  3. if element exists in array, leave it.
  4. and if it does not exists, print it.
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
vivek
  • 404
  • 1
  • 5
  • 21