Removing elements that have consecutive duplicates

后端 未结 9 1237
眼角桃花
眼角桃花 2020-11-22 10:02

I was curios about the question: Eliminate consecutive duplicates of list elements, and how it should be implemented in Python.

What I came up with is this:

9条回答
  •  伪装坚强ぢ
    2020-11-22 10:41

    A "lazy" approach would be to use itertools.groupby.

    import itertools
    
    list1 = [1, 2, 3, 3, 4, 3, 5, 5]
    list1 = [g for g, _ in itertools.groupby(list1)]
    print(list1)
    

    outputs

    [1, 2, 3, 4, 3, 5]
    

提交回复
热议问题