​列表去重是Python中一种常见的处理方式，任何编程场景都可能会遇到需要列表去重的情况。

```from random import randrange

DUPLICATES = [randrange(100) for _ in range(1000000)]
```

1.新建一个数组，遍历原数组，如果值不在新数组里便加入到新数组中。

```# 第一种方式
def easy_way():
unique = []
for element in DUPLICATES:
if element not in unique:
unique.append(element)
return unique```

```%timeit easy_way()
# 1.16 s ± 137 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)```

```def easy_way():
unique = set()
for element in DUPLICATES:
if element not in unique:
return unique```
```%timeit easy_way()
# 48.4 ms ± 11.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)```

2.直接对数组进行集合转化，然后再转回数组：

```# 第二种去重方式
def fast_way()
return list(set(DUPLICATES))```

```%timeit fast_way()
# 14.2 ms ± 1.73 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)```

3.保留原有数组顺序的去重

```def save_order():
return list(dict.fromkeys(DUPLICATES))```

```%timeit save_order()
# 39.5 ms ± 8.66 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)```

4. Python3.6以下的列表保留顺序去重

```from collections import OrderedDict
def save_order_below_py36():
return list(OrderedDict.fromkeys(DUPLICATES))```

```%timeit save_order_below_py36()
# 71.8 ms ± 16.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)```

​Python实用宝典 ( pythondict.com )