Updated August 19, 2023
Introduction to Python Unique List
In Python programming language, the list is defined as a set of elements in some order, which also allows duplicate numbers to get unique numbers or elements from the given list. We introduce unique lists. Python’s unique list is a list that contains unique elements irrespective of the order. There are several methods to get a unique list from the given list. Let’s see the below example of what does the unique list contains.
Example:
- Consider the given input list A = [76, 67, 86, 76, 86, 89, 98, 67, 67]
- Output: A = [76, 67, 86, 89, 98]
From the above example, we can see that in the given list A there are elements or numbers which have duplicate elements, so in order to get a unique list, it gives the output without duplicate elements.
What are the Methods to get Unique List in Python?
Below are the methods:
Method #1 – Using the Traversal
In this method, the items on the list are traversed individually. Then the items are added or can append the item in the unique list if the item is not in a unique list. This can be done by simple for loop and if statements. Below is the code for this method.
Code:
def python_unique(list_1):
unique_list = []
for n in list_1:
if n not in unique_list:
unique_list.append(n)
for n in unique_list:
print n,
list_1 = [76, 67, 86, 76, 86, 89, 98, 67, 67]
print("The Python unique list is")
python_unique(list_1)
Output:
Method #2 – Using set () property
set(), which has iterable arguments which are distinct elements. Using this property checks for the unique items and appends these unique items of the list to set. The main difference between list and set is that the list allows us to insert duplicate elements, but the set does not have duplicate elements as the elements are inserted only once, even if it’s inserted more than once.
Syntax:
set(Iterable elements)
Code:
def python_unique(list_2):
set_list = set (list_2)
unique_list2 = ( list (set_list))
for n in unique_list2:
print n
list_2 = [76, 67, 86, 76, 86, 89, 98, 67, 67]
print("The Python unique list is")
python_unique(list_2)
Output:
Method #3 – Using Python Library numpy
In python, we have very powerful libraries that make our job done easily. Firstly import the numpy library. Then convert the given list to an array using a=numpy.array() and use numpy.unique(a) functions that return the unique list of elements from the given list.
Code:
import numpy as nump
def python_unique (list_3):
n = nump.array(list_3)
print( nump.unique (n) )
list_3 = [76, 67, 86, 76, 86, 89, 98, 67, 67]
print(" The Python unique list is")
python_unique(list_3)
Output:
How Unique Function works in Python?
From the above methods, we used numpy.unique() function. In Python, a unique () function is a function that returns the unique values from the given lists or array of values.
This can be done as follows:
Syntax:
Numpy.unique( arr, return_index, return_inverse, return_counts)
Explanation:
- arr: It contains the input array of elements.
- retur_index: Returns the indices of the input array elements if true.
- return_inverse: Returns the indices of the unique array elements used to get the unique list or array as a new input array, if true
- return_count: It returns the count of how many times the duplicate are present in the input array from the unique array elements.
Let us see the working of this function with the help of the examples mentioned below:
Example #1
Code:
import numpy as nump
a = nump.array([76,67,86,76,86,89,98,67,67]
print 'Input array:'
print a
print '\n'
print 'Unique values of input array:'
u = nump.unique(a)
print u
print '\n'
print 'Unique array and Indices array:'
u,indices = nump.unique(a, return_index = True)
print indices
print '\n'
print 'Indices of unique array:'
u,indices = nump.unique(a,return_inverse = True)
print u
print '\n'
print 'Indices are:'
print indices
print '\n'
print 'Elements obtained from the indices:'
print u[indices]
print '\n'
print 'Count of duplicate elements:'
u,indices = nump.unique(a,return_counts = True)
print u
print indices
Output:
Example #2
As we know, Python is used with Data Science which helps to get some unique values for analysis from the huge data set. To do this in python, we use the Pandas package, which has the unique () function to get unique values from the given data set, which is a CSV file.
Code:
import pandas as ps
data_set = ps.read_csv( "articles.csv")
unique_arr = data_set["Python"].unique()
print (unique_arr)
Explanation: From the example, first import pandas and use the unique function. Read the file articles.csv from which the unique word “Python” needs to be printed.
Conclusion
In the Python programming language, there are several methods to get unique values from the given list, array of elements, data sets, etc. Unique values are obtained by traversing every element in the list and comparing them with the input array, and appending only the unique values in a unique list. Unique values are also obtained by using the set() property on a list or array of elements where duplicate elements are not allowed in set(). Unique data can be obtained from python packages or libraries like numpy and Pandas, which have the unique() function to get the output of unique data from the given set of data.
Recommended Articles
This is a guide to Python Unique List. Here we discuss the introduction to Python Unique List along with different methods to get a unique list and respective examples for better understanding. You can also go through our other related articles to learn more –