5 Best Ways to Convert a Python List to a Unique List

πŸ’‘ Problem Formulation: Python programmers commonly encounter the need to remove duplicates from a list, ensuring that each element is unique. The task is to take an input list, like [1, 2, 2, 3, 3, 3, 4], and produce an output list with duplicates removed, such as [1, 2, 3, 4]. This article discusses multiple methods to achieve a list with distinct elements in Python.

Method 1: Using a Set

Transforming a list into a set is the most straightforward way to remove duplicates, as sets inherently contain unique elements by definition. Converting back to a list yields the desired result. This method is fast and efficient but does not preserve the original order of elements.

Here’s an example:

original_list = [1, 2, 2, 3, 3, 3, 4]
unique_list = list(set(original_list))
print(unique_list)

Output: [1, 2, 3, 4]

This code snippet takes our original list, converts it to a set to eliminate duplicates, and then converts it back to a list. Keep in mind that the order of elements is not guaranteed to be the same as the original.

Method 2: Using List Comprehension and ‘in’

List comprehensions offer an elegant way to create lists in Python. By checking if an element is ‘in’ a temporary list, we can exclude duplicates, effectively building a list of unique elements while preserving the original order.

Here’s an example:

original_list = [4, 4, 5, 5, 5, 6]
unique_list = []
[unique_list.append(x) for x in original_list if x not in unique_list]
print(unique_list)

Output: [4, 5, 6]

This snippet iterates over theoriginal list and appends each element to the unique_list only if it is not already present, ensuring that all elements in the resulting list are unique.

Method 3: Using OrderedDict

‘collections.OrderedDict’ tracks the order in which keys are added. If you use a list’s elements as keys, you can maintain the list’s order while removing duplicates by simply converting the list to an OrderedDict and back to a list.

Here’s an example:

from collections import OrderedDict
original_list = [3, 1, 2, 3, 1, 2, 2, 4]
unique_list = list(OrderedDict.fromkeys(original_list))
print(unique_list)

Output: [3, 1, 2, 4]

The OrderedDict.fromkeys() method creates an OrderedDict with list elements as keys, discarding any duplicates and preserving order. The keys are then converted back into a list.

Method 4: Using a Loop

Employing a simple loop to iterate through the list and append elements to a new list if they haven’t already been added is a more manual but straightforward approach, which also keeps the original ordering intact.

Here’s an example:

original_list = [7, 8, 9, 7, 8, 10]
unique_list = []
for item in original_list:
    if item not in unique_list:
        unique_list.append(item)
print(unique_list)

Output: [7, 8, 9, 10]

In this piece of code, we loop through each item of the original_list and add it to unique_list only if it does not already exist in the unique_list, resulting in a list of unique items only.

Bonus One-Liner Method 5: Using a Function and List Comprehension (Python 3.7+)

Python 3.7 introduced guaranteed dictionary order, which can be exploited with a combination of list comprehension and a function to produce a concise one-line command to remove duplicates and preserve order.

Here’s an example:

unique_list = lambda l: list(dict.fromkeys(l))
print(unique_list([10, 10, 11, 11, 12, 12]))

Output: [10, 11, 12]

This one-liner defines an anonymous function that uses dict.fromkeys() to remove duplicates, taking advantage of the data type’s ordering property in Python 3.7+ and then converts the dictionary back to a list.

Summary/Discussion

  • Method 1: Using a Set. Fast and efficient; does not maintain order.
  • Method 2: Using List Comprehension and ‘in’. Preserves order; may be less efficient for large lists due to repeated ‘in’ checks.
  • Method 3: Using OrderedDict. Preserves order; requires importing a module; relatively efficient.
  • Method 4: Using a Loop. Simple and readable; preserves order; potential inefficiency with large lists.
  • Bonus Method 5: One-Liner with Dictionary Order. Compact and elegant; preserves order; relies on Python 3.7+ ordering behavior.