Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Python List Unique in Functional way (map/reduce/filter)

Is there a way in Python of making a List unique through functional paradigm ?

Input : [1,2,2,3,3,3,4]

Output: [1,2,3,4] (In order preserving manner)

I know there are other ways but none is in the functional way.

like image 217
Sibi Avatar asked Dec 21 '22 12:12

Sibi


1 Answers

If you need to just delete adjacent occurrences try this:

reduce(lambda x,y: x+[y] if x==[] or x[-1] != y else x, your_list,[])

If you need to delete all but one ocurrence try this:

reduce(lambda x,y: x+[y] if not y in x else x, your_list,[])
like image 99
kelwinfc Avatar answered Dec 24 '22 15:12

kelwinfc