Ways to remove duplicates from a given list

Python ways of getting a list of unique elements from a list that possibly contains duplicates

We may use a wide range of approaches to solve this problem, common ways incl.:

  1. set (when seq order is not a concern)
  2. simply looping through both the original list and the result list, we can further modify the lines to use list comprehension:
res = []
for e in ori_list:
	if e not in res:
		res.append(e)
# list comprehension
# ori_list = [2, 2, 3, 5, 7, 11, 11, 11, 13]
res = []
[res.append(e) for e in ori_list if e not in res] # no assignment!
>>> res
[2, 3, 5, 7, 11, 13]
  1. The use of OrderedDict, built-in, efficient
from collections import OrderedDict
res = list(OrderedDict.fromkeys(ori_list))
  1. Using enumerate()
res = [v for i, v in enumerate(ori_list) if v not in ori_list[:i]]

We noticed that if the duplicates are consecutive elements, we can also use the following methods from itertools:

  1. groupby() + list comprehension
from itertools import groupby
res = [item[0] for item in groupby(ori_list)]
  1. zip_longest() + list comprehension
from itertools import zip_longest
res = [i for i, j in zip_longest(ori_list, ori_list[1:]) if i != j]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值