- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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 .
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.
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))
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.
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))
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.
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))
Following is an output of the above code −
[2, 4, 6, 8]
- Related Articles
- C# program to print unique values from a list
- Java program to print unique values from a list
- Get unique values from a list in Python
- Python program to print duplicates from a list of integers?
- Python program to mask a list using values from another list
- Python Program to get all unique keys from a List of Dictionaries
- How to subset unique values from a list in R?
- Python Program to print element with maximum vowels from a List
- Program to find total unique duration from a list of intervals in Python
- Python Pandas - Get unique values from a column
- Python Program to Print Nth Node from the last of a Linked List
- Python program to print even numbers in a list
- Python program to print negative numbers in a list
- Python program to print odd numbers in a list
- Python program to print all sublists of a list.