Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove List duplicates in Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to remove List duplicates in Python". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to remove List duplicates in Python" can help you solve your doubts.

Method 1: naive method

This way is to add the first element that appears in the new list based on traversing the entire list.

Sample code:

# Python 3 code to demonstrate # removing duplicated from list # using naive methods # initializing listtest_list = [1,3,5,6,3,5,6,1] print ("The original list is:" + str (test_list)) # using naive method# to remove duplicated # from list res = [] for i in test_list: if i not in res: res.append (I) # printing list after removal print ("The list after removing duplicates:" + str (res))

→ output result:

The original list is: [1, 3, 5, 6, 3, 5, 6, 1]

The list after removing duplicates: [1, 3, 5, 6]

Method 2: list analytic expression

This approach is actually a simplified version of the first method, which uses a list parser and replaces the loop above with a single line of code.

Sample code:

# Python 3 code to demonstrate # removing duplicated from list # using list comprehension# initializing listtest_list = [1,3,5,6,3,5,6,1] print ("The original list is:" + str (test_list)) # using list comprehension# to remove duplicated # from list res = [] [res.append (x) for x in test_list if x not in res] # printing list after removal print ("The list after removing duplicates:" + str (res))

→ output result:

The original list is: [1, 3, 5, 6, 3, 5, 6, 1]

The list after removing duplicates: [1, 3, 5, 6]

Method 3: use set ()

This is the most popular way to remove duplicate elements from the list. But one of the biggest drawbacks of this approach is that the order of the elements in the list is no longer the same after use.

Sample code:

# Python 3 code to demonstrate # removing duplicated from list # using set () # initializing listtest_list = [1,5,3,6,3,5,6,1] print ("The original list is:" + str (test_list)) # using set () # to remove duplicated # from listtest_list = list (set (test_list)) # printing list after removal # distorted orderingprint ("The list after removing duplicates:" + str (test_list))

→ output result:

The original list is: [1, 5, 3, 6, 3, 5, 6, 1]

The list after removing duplicates: [1, 3, 5, 6]

Method 4: use list analytic expression + enumerate ()

This method uses enumerations to remove duplicate elements on the basis of list analytic expressions. Skip the element by checking whether it already exists in the list. This method keeps the order of the elements in the list unchanged.

Sample code:

# Python 3 code to demonstrate # removing duplicated from list # using list comprehension + enumerate () # initializing listtest_list = [1,5,3,6,3,3,5,6,1] print ("The original list is:" + str (test_list)) # using list comprehension + enumerate () # to remove duplicated # from list res = [i for n, i in enumerate (test_list) if i not in test_list [: n]] # printing list after removal print ("The list after removing duplicates:" + str (res))

→ output result:

The original list is: [1, 5, 3, 6, 3, 5, 6, 1]

The list after removing duplicates: [1, 5, 3, 6]

Method 5: using collections.OrderedDict.fromkeys ()

This is the fastest way to accomplish a special task. It first removes duplicates from the list and returns a dictionary, and finally converts it to a list. This method can also handle strings.

Sample code:

# Python 3 code to demonstrate # removing duplicated from list # using collections.OrderedDict.fromkeys () from collections import OrderedDict # initializing listtest_list = [1,5,3,6,3,5,6,1] print ("The original list is:" + str (test_list)) # using collections.OrderedDict.fromkeys () # to remove duplicated # from list res = list (OrderedDict.fromkeys (test_list)) # printing list after removal print ("The list after removing duplicates:" + str (res))

→ output result:

The original list is: [1, 5, 3, 6, 3, 5, 6, 1]

The list after removing duplicates: [1, 5, 3, 6]

Method 6: deal with repeating elements in nested lists

Remove repeating elements from a multidimensional list (list nesting). It is assumed that the elements in the list (also the list) that have the same elements (but not necessarily in the same order) are treated as repeating elements. So let's use the set () + sorted () method to complete the task.

Sample code:

# Python3 code to demonstrate# removing duplicate sublist # using set () + sorted () # initializing listtest_list = [[1,0,1], [- 1,0,1], [- 1,0,1], [1,2,3], [3,4] 1]] # printing original listprint ("The original list:" + str (test_list)) # using set () + sorted () # removing duplicate sublistres = list (set (tuple (sorted (sub)) for sub in test_list)) # print resultprint ("The list after duplicate removal:" + str (res))

→ output result:

The original list: [[1,0,-1], [- 1,0,1], [- 1,0,1], [1,2,3], [3,4,1]]

The list after duplicate removal: [(- 1,0,1), (1,3,4), (1,2,3)]

You can also use set () + map () + sorted ()

Sample code:

# Python3 code to demonstrate# removing duplicate sublist # using set () + map () + sorted () # initializing listtest_list = [[1,0,-1], [- 1,0,1], [- 1,0,1], [1,2,3], [3,4] 1]] # printing original listprint ("The original list:" + str (test_list)) # using set () + map () + sorted () # removing duplicate sublistres = list (set (map (lambda I: tuple (sorted (I)), test_list)) # print resultprint ("The list after duplicate removal:" + str (res))

→ output result:

The original list: [[1,0,-1], [- 1,0,1], [- 1,0,1], [1,2,3], [3,4,1]]

The list after duplicate removal: [(- 1,0,1), (1,3,4), (1,2,3)]

After reading this, the article "how to remove List duplicates in Python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report