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 ‘[element 1, element 2,….]’. 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 to get unique values

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))

Output

Following is an output of the above code −s

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

Using list.append() with for loop to get unique values

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))

Output

Following is an output of he above code

['apple', 'orange', 'mango']

Using numpy.unique() method to get unique values

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))

Output

Following is an output of the above code −

[2, 4, 6, 8]

Updated on: 23-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements