Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to print unique values from a list
List is a built-in data structure in python which functions like a dynamically sized array. Lists are created by putting elements in square brackets [element1, element2, ....]. Elements present in list are indexed and the indexing starts from [0].
List has the following properties ?
-
List is mutable meaning that elements can be added, removed or changed from a list after its creation.
-
List is ordered, so adding new elements to the list will not change the order of existing elements.
-
List allows for entry of duplicate elements since each element has a unique index.
-
A single list can contain multiple data types.
Now, let us see various ways to print unique values from a list using python program ?
Using set() Method
In python the set() method stores only a single copy of all elements present in it. When a list with duplicate elements is converted to set, only a single copy of the duplicate values is stored. The set can then be converted back to a list to create a list with only unique values.
Example
Following is an example of set() method to print unique values from a list ?
def unique_numbers(duplicate): # converting list to set to store unique values unique_numbers_set = set(duplicate) # converting the set back to a list unique_numbers_list = list(unique_numbers_set) return unique_numbers_list duplicate_list = [1, 2, 3, 4, 1, 2, 5, 6, 6, 6] print(unique_numbers(duplicate_list))
The output of the above code is ?
[1, 2, 3, 4, 5, 6]
Using list.append() with For Loop
The append() method in python is used to add elements to the last index of the list. First an empty list is created to store the unique elements of the list containing duplicate values.
A for loop is used with an if condition to check if element at ith position is present in the empty list. If the element is not present, then it is appended to the list. Hence, only unique elements are present because if duplicate is encountered the if condition fails and the duplicate element is not appended.
Example
Following is an example of list.append() with for loop to get unique values from a list ?
def unique_fruits(fruit):
# create an empty list
unique_fruit = []
# for loop to traverse the list elements
for traverse in fruit:
# check if element not present in list
if traverse not in unique_fruit:
unique_fruit.append(traverse) # append the unique element
return unique_fruit
fruits = ['apple', 'orange', 'mango', 'apple', 'orange', 'mango']
print(unique_fruits(fruits))
The output of the above code is ?
['apple', 'orange', 'mango']
Using numpy.unique() Method
NumPy is a python library that supports multidimensional array objects and is mainly used for advanced mathematical operations on large datasets for scientific computing.
The numpy.unique() method is a built-in method of NumPy library that is used to return unique values from an array in sorted order. After converting the duplicate list to an array, the unique() method is used to remove duplicates and then the array can be converted back to a list.
Example
Following is an example of numpy.unique() method to get unique values from a list ?
import numpy as np def unique(duplicate): # create an array from the list array = np.array(duplicate) # unique method returns in unique elements in sorted order unique_elements = np.unique(array) # convert the array to list sorted_unique_list = list(unique_elements) return sorted_unique_list duplicate_list = [8, 4, 2, 4, 2, 8, 6, 6] print(unique(duplicate_list))
The output of the above code is ?
[2, 4, 6, 8]
Comparison of Methods
| Method | Preserves Order? | Performance | Best For |
|---|---|---|---|
set() |
No | Fast | Simple unique extraction |
append() loop |
Yes | Slow (O(n²)) | Maintaining order |
numpy.unique() |
No (sorts) | Very fast | Large datasets, sorted output |
Conclusion
Use set() for fastest unique value extraction. Use the loop method when preserving original order is important. Use numpy.unique() for large datasets or when you need sorted unique values.
