-1

I wish to trim the list

[0,0,0,0,0,0,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3, ... , 145,145,145]

into

[0,1,2,3,4,...,145]

I know the "C-Programming-style" way of doing it in Python.

I am here to ask for an intelligent and smart way of doing this.

Sibbs Gambling
  • 18,128
  • 38
  • 90
  • 168

3 Answers3

1

numpy.unique(mylist) or list(set(mylist)) should do.

PS And it is not 'trimming' - that's a different thing...

sashkello
  • 16,318
  • 22
  • 78
  • 104
1

Use set:

unique = [x for x in set(original)]

or

unique = list(set(original))
jason
  • 228,647
  • 33
  • 413
  • 517
0

Give this a try

print list(set(my_list))

scohe001
  • 14,655
  • 2
  • 30
  • 50