-3

I want to remove those element from list which has repetition more than one it's like set but order should not be change

[4,6,2,6,1,2] should become [4,6,2,1]

I'am looking for any inbuilt method or list comprehension

Nikola
  • 411
  • 1
  • 5
  • 9
  • 1
    Sets don't have an order. – Klaus D. May 30 '19 at 17:10
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is not a design, coding, research, or tutorial resource. – Prune May 30 '19 at 17:11
  • Similar question that can help you https://stackoverflow.com/questions/9792664/converting-a-list-to-a-set-changes-element-order – Ander2 May 30 '19 at 17:18
  • Since Python 3.7 you can do `stableDistinct = dict.fromkeys(data,"").keys()` – Alain T. May 30 '19 at 22:30

1 Answers1

0

This should do it: use lambda function and remove duplicates using set. Tested on Python 2.7

mylist = [4,6,2,6,1,2]
reduce(lambda r, v: v in r[1] and r or (r[0].append(v) or r[1].add(v)) or r, mylist, ([], set()))[0]
MEdwin
  • 2,940
  • 1
  • 14
  • 27
  • I think a fair amount of explanation is needed for this code snippet to be genuinely useful, otherwise, it's fairly hard to read. Also, in case OP is using python3, `from functools import reduce` will be needed at the top – C.Nivs May 30 '19 at 17:22