Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

How to remove duplicate items from list in Python?

The difference between the list and the Set is that an element can appear more than once in a list, but an element can appear only once in a set. Hence, if we cast a list to a set, duplicates will be removed. However, the original order of elements is not guaranteed. The order of elements in a set is decided by hashing mechanism, which may be different than in the list. This is verified by the following code:

Example: List to Set
>>> mylist=[5,10,15,20,3,15,25,20,30,10,100] >>> myset=set(mylist) >>> print(list(myset)) [3, 100, 5, 10, 15, 20, 25, 30]

So, how to remove duplicate appearance yet retain original order?

Append Unique Items in another List using For Loop

A simple approach would be to append the first appearance of each number in another list, as shown below.

Example: Append Unique Item into Another List
>>> uniques=[] >>> for num in mylist: if num not in uniques: uniques.append(num) >>> print(uniques) [5, 10, 15, 20, 3, 25, 30, 100]

Using List Comprehension

We can use list comprehension to make it little more concise.

Example:
>>> uniques=[] >>> [uniques.append(num) for num in mylist if not num in uniques] >>> print(uniques) [5, 10, 15, 20, 3, 25, 30, 100]

The above approach is simple in implementation but not efficient, especially for a list with the large number of items. The following technique removes duplicates fairly efficiently.

Using OrderedDict.fromkeys()

The solution is slightly different from Python versions less than 3.7 and later versions. Prior to Python 3.7, dictionary output may not be as per the order of insertion. However, OrderedDict can do so. We then use the fromkeys() method to build an ordered dictionary using list items as keys whose associated value is None.

Example: OrderedDict.fromkeys()
>>> mylist=[5,10,15,20,3,15,25,20,30,10,100] >>> from collections import OrderedDict >>> list(OrderedDict.fromkeys(mylist)) [5, 10, 15, 20, 3, 25, 30, 100]

In later versions, dictionary is guaranteed to remember its key insertion order. Hence, the fromkeys() method of normal dict class would do the same job.

Using the reduce() Function

The most efficient solution to this problem is to use the reduce() function of the functools module.

In the following example, a two-element tuple with an empty list and set is used as an initializer. Each new occurrence in the original list is appended in an empty list, and Set acts as a look-up table.

Example: reduce()
>>> from functools import reduce >>> mylist=[5,10,15,20,3,15,25,20,30,10,100] >>> tup = (list(), set()) >>> # list to keep order, set as lookup table >>> def myfunction(temp, item): if item not in temp[1]: temp[0].append(item) temp[1].add(item) return temp >>> uniques=reduce(myfunction, mylist, tup)[0] >>> print(uniques) [5, 10, 15, 20, 3, 25, 30, 100]
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.