Tutorials  /  Python

Get Unique Values From a List in Python

Ccentron Redaktion · November 2024 ·4 min read ·Python, Tutorial

In this article, we will be understanding 3 ways to get unique values from a Python list. While dealing with a huge amount of raw data, we often come across situations wherein we need to fetch out the unique and non-repeated set of data from the raw input data-set.

VM

Try it yourself: cloud servers from €3.12/month

ccloud³ VMs from German data centers – deployed in seconds, billed by the hour, with a €200 starting credit for new accounts. Launch a server →

The methods listed below will help you solve this problem. Let’s get right into it!

Ways to Get Unique Values from a List in Python

Either of the following ways can be used to get unique values from a list in Python:

  • Python set() method
  • Using Python list.append() method along with a for loop
  • Using Python numpy.unique() method

1. Python Set() to Get Unique Values from a List

As seen in our previous tutorial on Python Set, we know that Set stores a single copy of the duplicate values into it. This property of set can be used to get unique values from a list in Python.

Initially, we will need to convert the input list to set using the set() function.

Syntax:

Code
set(input_list_name)

As the list gets converted to set, only a single copy of all the duplicate elements gets placed into it. Then, we will have to convert the set back to the list using the below command/statement:

Syntax:

Code
list(set-name)

Finally, print the new list

Example:

Python
list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

set_res = set(list_inp) 
print("The unique elements of the input list using set():\n") 
list_res = (list(set_res))
 
for item in list_res: 
    print(item) 

Output:

Code
The unique elements of the input list using set():

25
75
100
20
12

2. Python list.append() and for loop

In order to find the unique elements, we can apply a Python for loop along with list.append() function to achieve the same.

At first, we create a new (empty) list i.e res_list. After this, using a for loop we check for the presence of a particular element in the new list created (res_list). If the element is not present, it gets added to the new list using the append() method.

Syntax:

Code
list.append(value)

In case, we come across an element while traversing that already exists in the new list i.e. a duplicate element, in such case it is neglected by the for loop. We will use the if statement to check whether it’s a unique element or a duplicate one.

Example:

Python
list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

res_list = []

for item in list_inp: 
    if item not in res_list: 
        res_list.append(item) 

print("Unique elements of the list using append():\n")    
for item in res_list: 
    print(item) 

Output:

Code
Unique elements of the list using append():

100
75
20
12
25

3. Python numpy.unique() function To Create a List with Unique Items

Python NumPy module has a built-in function named, numpy.unique to fetch unique data items from a numpy array.

In order to get unique elements from a Python list, we will need to convert the list to NumPy array using the below command:

Syntax:

Code
numpy.array(list-name)

Next, we will use the numpy.unique() method to fetch the unique data items from the numpy array and finally, we’ll print the resulting list.

Syntax:

Code
numpy.unique(numpy-array-name)

Example:

Python
import numpy as N
list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

res = N.array(list_inp) 
unique_res = N.unique(res) 
print("Unique elements of the list using numpy.unique():\n")
print(unique_res)

Output:

Code
Unique elements of the list using numpy.unique():

[12  20  25  75 100]

Conclusion

In this article, we have unveiled various ways to fetch the unique values from a set of data items in a Python list.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren