How to Remove Duplicates From Lists in Python

Avatar

By squashlabs, Last Updated: Nov. 2, 2023

How to Remove Duplicates From Lists in Python

Removing duplicates from a list is a common task in Python programming. There are multiple ways to achieve this, depending on your requirements and the characteristics of the list. In this answer, we will explore two popular and efficient methods to remove duplicates from lists in Python.

Method 1: Using the set() Function

One of the simplest and most straightforward ways to remove duplicates from a list is by converting it to a set. The set data structure in Python does not allow duplicate elements, so converting the list to a set will automatically remove any duplicates. After removing the duplicates, you can convert the set back to a list if needed.

Here's an example that demonstrates how to use the set() function to remove duplicates from a list:

my_list = [1, 2, 3, 4, 2, 3, 5, 6, 4, 7, 8, 5]
unique_list = list(set(my_list))
print(unique_list)

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

In this example, we have a list called my_list that contains duplicate elements. By converting it to a set using the set() function and then back to a list using the list() function, we obtain a new list called unique_list that contains only the unique elements from the original list.

It's important to note that the order of the elements may change when converting a list to a set and back to a list, as sets do not preserve the order of elements. If you need to preserve the order of the elements, you can use the second method described below.

Related Article: How to Uninstall All Pip Packages in Python

Method 2: Using a List Comprehension

Another popular method to remove duplicates from a list is by using a list comprehension. List comprehensions provide a concise and efficient way to create new lists based on existing lists. By iterating over the original list and only adding elements to the new list if they have not been added before, we can effectively remove duplicates.

Here's an example that demonstrates how to use a list comprehension to remove duplicates from a list while preserving the order of the elements:

my_list = [1, 2, 3, 4, 2, 3, 5, 6, 4, 7, 8, 5]
unique_list = []
[unique_list.append(x) for x in my_list if x not in unique_list]
print(unique_list)

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

In this example, we initialize an empty list called unique_list. The list comprehension iterates over each element x in the original list my_list. If the element is not already present in unique_list, it gets appended to the list. This ensures that only unique elements are added to the new list.

Using a list comprehension can be more memory-efficient compared to converting a list to a set if the original list is large, as it avoids creating a temporary set in memory. However, it may be slightly slower for smaller lists due to the additional check for element presence.

Additional Considerations

Related Article: How to Check If Something Is Not In A Python List

- If your list contains mutable objects like lists or dictionaries, the above methods will only remove duplicates based on the object's identity, not its contents. If you need to remove duplicates based on the contents of mutable objects, you can convert them to immutable objects like tuples before applying the above methods.

- If you want to remove duplicates from a list without changing its order and also preserve the original list, you can create a copy of the list and apply either of the above methods to the copy.

- If you need to remove duplicates from a list while preserving the order and also count the number of occurrences of each element, you can use the collections.Counter class from the Python standard library.

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How to Convert JSON to CSV in Python

This article provides a guide on how to convert JSON to CSV using Python. Suitable for all levels of expertise, it covers two methods: using the json… read more

How To Create Pandas Dataframe From Variables - Valueerror

Constructing a Pandas dataframe from variables in Python can sometimes result in a ValueError, especially when using only scalar values and no index.… read more

How to Use Python Pip Install on MacOS

This guide provides step-by-step instructions for installing Python pip package manager on MacOS. It covers topics such as installing Homebrew, setti… read more

Tutorial: i18n in FastAPI with Pydantic & Handling Encoding

Internationalization (i18n) in FastAPI using Pydantic models and handling character encoding issues is a crucial aspect of building multilingual APIs… read more

How to End Python Programs

This guide provides software developers with essential information on correctly terminating Python programs. It covers various methods for ending Pyt… read more

How to Download a File Over HTTP in Python

Guide on using Python to download a file from a URL via HTTP. Learn how to download files using the requests library and the urllib module. Best prac… read more

How To Get Substrings In Python: Python Substring Tutorial

Learn how to extract substrings from strings in Python with step-by-step instructions. This tutorial covers various methods, including string slicing… read more

Optimizing FastAPI Applications: Modular Design, Logging, and Testing

Learn best practices in FastAPI for designing modular applications, logging, and testing. This article dives into the key aspects of optimizing FastA… read more

How to Change Column Type in Pandas

Changing the datatype of a column in Pandas using Python is a process. This article provides a simple guide on how to change column types in Pandas u… read more

Converting Integer Scalar Arrays To Scalar Index In Python

Convert integer scalar arrays to scalar index in Python to avoid the 'TypeError: Only integer scalar arrays can be converted to a scalar index with 1… read more