0

I have lists containing sequence of numbers like below:

list_1 = [4.5 ,4.5 ,4, 4 ,4, 5, 5, 5]
list_2 = [6.5 ,7 ,7 ,8 ,8]
list_3 = [14.5 ,15 ,15 , 15.5]

I want to remove duplicates from those list in a way that if list contain repetition keep only one occurrence, and preserve the order:

list_1 = [4.5 ,4, 5]
list_2 = [6.5 ,7 ,8]
list_3 = [14.5 ,15 , 15.5]
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
max scender
  • 113
  • 6

1 Answers1

3

If order does not matter, cast them to a set.

list_1 = [4.5 ,4.5 ,4, 4 ,4, 5, 5, 5]
list_2 = [6.5 ,7 ,7 ,8 ,8]
list_3 = [14.5 ,15 ,15 , 15.5]

list_1 = list(set(list_1))
list_2 = list(set(list_2))
list_3 = list(set(list_3))

If order does matter, use a de-duplication function.

def dedupe(lst):
    items = []
    for item in lst:
        if item not in items:
            items.append(item)
    return items
chrisharrel
  • 337
  • 2
  • 10