Python中如何删除列表中的重复项?

Python中如何删除列表中的重复项?

要从 Python 中的列表中删除重复项,我们可以使用本文中讨论的各种方法。

使用字典从列表中删除重复项

示例

在此示例中,我们将使用 OrderedDict 从列表中删除重复项 -

from collections import OrderedDict 1. Creating a List with duplicate items mylist = ["Jacob", "Harry", "Mark", "Anthony", "Harry", "Anthony"] 1. Displaying the List print("List = ",mylist) 1. Remove duplicates from a list using dictionary resList = OrderedDict.fromkeys(mylist) 1. Display the List after removing duplicates print("Updated List = ",list(resList)) 登录后复制