0

I have a list and want delete repeat same items.

list1 = [ 10-31, 19-45, 19-45, 64-01, 10-31, 19-45, 16-17 ]

And try get this:

list2 = [ 10-31, 19-45, 64-01, 10-31, 19-45,16-17]

I already looked list(OrderedDict.fromkeys(data1)) - method, but that's not what I want.

David Grigoryev
  • 125
  • 1
  • 11
  • You mean you want to remove duplicates, or remove duplicates only if they are one-after-another? – Idos May 07 '18 at 08:15
  • Please take a look at this: https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Chris Aby Antony May 07 '18 at 08:16

3 Answers3

1

You can take advantage of itertools.groupby:

from itertools import groupby

list1 = ['10-31', '19-45', '19-45', '64-01', '10-31', '19-45', '16-17']
list2 = [x[0] for x in groupby(list1)]
list2
#['10-31', '19-45', '64-01', '10-31', '19-45', '16-17']
zipa
  • 27,316
  • 6
  • 40
  • 58
0

Try this:

list2 = list(set(list1))
0

You can use set to do this:

list1 = [ 10-31, 19-45, 19-45, 64-1, 10-31, 19-45, 16-17 ]
list2 = [ 10-31, 19-45, 64-2, 10-31, 19-45,16-17]
set1 = set(list1)
set2 = set(list2)
set3 = set1 | set2
YuhaoQI
  • 69
  • 7