0

Like the given list is l=[4,2,3,4,4,5]

Turn into l=[4,2,3,4,5]

I try to create an empty list to store the value but it is hard to compare if the two elements are equal with no loop

Harry
  • 1
  • 1

2 Answers2

0

to store every element only once is same like a set. you can use following code.

l=[2,3,4,4,5]
l=list(set(l))
l.sort()

it will give your expected output

0

You can use a dictionary which only keeps unique values as its key

l=[2,3,4,4,5]

list(dict.fromKeys(l))
greg-449
  • 109,219
  • 232
  • 102
  • 145
Nobi
  • 1,113
  • 4
  • 23
  • 41
  • Would it be made into a key? I just want to check the adjacent values are equal or not – Harry Apr 20 '19 at 06:09
  • if removing duplicates is the goal then the outer list function converts the keys back to a list, but if you want to specifically check the adjacent values, you will need a loop. You probably want to reframe your topic then. – Nobi Apr 20 '19 at 06:14
  • Do you have any idea to use map() or reduce() to fix it? – Harry Apr 20 '19 at 06:20