Ask Your Question
1

How to eliminate duplicates in a list preserving previous order

asked 2022-10-26 11:05:53 +0100

logomath gravatar image

updated 2022-10-26 11:50:45 +0100

Ex: if A=[1,5,3,3,4,9,9,9,1] I want to obtain [1,5,3,4,9,1] in a simple way

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-10-26 13:34:45 +0100

Max Alekseyev gravatar image

For example via list comprehension:

B = [A[i] for i in range(len(A)) if i==0 or A[i]!=A[i-1]]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-10-26 11:05:53 +0100

Seen: 347 times

Last updated: Oct 26 '22