Python | Get unique values from a list - GeeksforGeeks (2024)

In this article, we will explore various techniques and strategies for efficiently extracting distinct elements from a given list. By delving into methods ranging from traditional loops to modern Pythonic approaches with Python.

Input : [1,2, 1, 1, 3, 4, 3, 3, 5]
Output : [1, 2, 3, 4, 5]
Explaination: The output only contains the unique element from the input list.

Get unique values from a list

Below are the topics that we will cover in this article:

  • Traversal of the list
  • Using Set method
  • Using reduce() function
  • Using Operator.countOf() method
  • Using pandas module
  • Using numpy.unique
  • Using collections.Counter()
  • Using dict.fromkeys()

Get Unique Values from a List by Traversal of the List

Using traversal, we can traverse for every element in the list and check if the element is in the unique_list already if it is not over there, then we can append it to the unique_list. This is done using one for loop and another if statement which checks if the value is in the unique list or not which is equivalent to another for a loop.

Python
# function to get unique valuesdef unique(list1): # initialize a null list unique_list = [] # traverse for all elements for x in list1: # check if exists in unique_list or not if x not in unique_list: unique_list.append(x) # print list for x in unique_list: print x,# driver codelist1 = [10, 20, 10, 30, 40, 40]print("the unique values from 1st list is")unique(list1)list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]print("\nthe unique values from 2nd list is")unique(list2)

Output

the unique values from 1st list is10 20 30 40 the unique values from 2nd list is1 2 3 4 5

Time Complexity: O(n*n)
Auxiliary Space: O(n)

Get Unique Values from a List Using Set Method

Using set() property of Python, we can easily check for the unique values. Insert the values of the list in a set. Set only stores a value once even if it is inserted more than once. After inserting all the values in the set by list_set=set(list1), convert this set to a list to print it.

Python
def unique(list1): # insert the list to the set list_set = set(list1) # convert the set to the list unique_list = (list(list_set)) for x in unique_list: print x,# driver codelist1 = [10, 20, 10, 30, 40, 40]print("the unique values from 1st list is")unique(list1)list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]print("\nthe unique values from 2nd list is")unique(list2)

Output

the unique values from 1st list is40 10 20 30 the unique values from 2nd list is1 2 3 4 5

Time complexity: O(n), where n is the length of a list.
Auxiliary Space: O(n), where n is the length of a list.

Get Unique Values From a List in Python Using reduce() function

Using Python import reduce() from functools and iterate over all element and checks if the element is a duplicate or unique value. Below is the implementation of the above approach.

Python
from functools import reducedef unique(list1): # Print directly by using * symbol ans = reduce(lambda re, x: re+[x] if x not in re else re, list1, []) print(ans)# driver codelist1 = [10, 20, 10, 30, 40, 40]print("the unique values from 1st list is")unique(list1)list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]print("\nthe unique values from 2nd list is")unique(list2)

Output

the unique values from 1st list is[10, 20, 30, 40]the unique values from 2nd list is[1, 2, 3, 4, 5]

Get Unique Values From a List in Python Using Operator.countOf() method

The ‘unique’ function initializes an empty ‘unique_list’, then iterates through ‘list1’. For each element ‘x’, it employs ‘op.countOf()‘ to check if ‘x’ is present in ‘unique_list’. If not found (count is 0), ‘x’ is appended to ‘unique_list’. The final unique values are printed using a loop. The driver code demonstrates this process for two lists, ‘list1’ and ‘list2’, showcasing the extraction of distinct elements from each list while maintaining their original order.

Python
import operator as op# function to get unique valuesdef unique(list1): # initialize a null list unique_list = [] # traverse for all elements for x in list1: # check if exists in unique_list or not if op.countOf(unique_list, x) == 0: unique_list.append(x) # print list for x in unique_list: print(x)# driver codelist1 = [10, 20, 10, 30, 40, 40]print("the unique values from 1st list is")unique(list1)list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]print("\nthe unique values from 2nd list is")unique(list2)

Output

the unique values from 1st list is10203040the unique values from 2nd list is12345

Time Complexity:O(N)
Auxiliary Space: O(N)

Get Unique Values From a List in Python Using pandas module

The ‘unique’ function utilizes Pandas to create a Series from ‘list1’, then employs ‘drop_duplicates()’ to eliminate duplicates and obtain a list of unique values. Subsequently, it iterates through the unique list and prints each element. The driver code demonstrates the process for two lists, ‘list1’ and ‘list2’, providing distinct values for each list.

Python
import pandas as pd# function to get unique valuesdef unique(list1): unique_list = pd.Series(list1).drop_duplicates().tolist() for x in unique_list: print(x)# driver codelist1 = [10, 20, 10, 30, 40, 40]print("the unique values from 1st list is")unique(list1)list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]print("\nthe unique values from 2nd list is")unique(list2)#This code is contributed by Vinay Pinjala.

Output:

the unique values from 1st list is
10
20
30
40
the unique values from 2nd list is
1
2
3
4
5

Time Complexity:O(N)
Auxiliary Space: O(N)

Get Unique Values From a List Using numpy.unique

Using Python’s import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy.array(list) and then use numpy.unique(x) function to get the unique values from the list. numpy.unique() returns only the unique values in the list.

Python
# using numpy.uniqueimport numpy as npdef unique(list1): x = np.array(list1) print(np.unique(x))# driver codelist1 = [10, 20, 10, 30, 40, 40]print("the unique values from 1st list is")unique(list1)list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]print("\nthe unique values from 2nd list is")unique(list2)

Output:

the unique values from 1st list is
[10 20 30 40]
the unique values from 2nd list is
[1 2 3 4 5]

Time complexity: O(nlogn) due to the use of the sorting algorithm used by the numpy.unique() function.
Auxiliary space: O(n) because numpy.unique() function creates a copy of the input array and then sorts it before returning the unique elements.

Get Unique Values From a List in Python Using collections.Counter()

Using Python to import Counter() from collections print all the keys of Counter elements or we print directly by using the “*” symbol. Below is the implementation of the above approach.

Python
from collections import Counter# Function to get unique valuesdef unique(list1): # Print directly by using * symbol print(*Counter(list1))# driver codelist1 = [10, 20, 10, 30, 40, 40]print("the unique values from 1st list is")unique(list1)list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]print("\nthe unique values from 2nd list is")unique(list2)

Output

the unique values from 1st list is10 20 30 40the unique values from 2nd list is1 2 3 4 5

Time Complexity: O(n), where n is the number of elements in the input list.
Auxiliary Space : O(n)

Get Unique Values From a List Using dict.fromkeys()

Using the fromkeys() method of dictionary data structure we can fetch the unique elements.Firstly we need to define a list that consists of duplicate elements.Then we need to use a variable in which we will store the result after using the fromkeys() method.We need to convert that result into a list, as the fromkeys() method is part of the dictionary so by default it returns a dictionary with all the unique keys and None as their values.

Python
# defining a list which consists duplicate valueslist1 = [10, 20, 10, 30, 40, 40]list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]# storing the result of the fromkeys()# operation and converting it into listunique_list_1 = list(dict.fromkeys(list1))unique_list_2 = list(dict.fromkeys(list2))# Printing the final resultprint(unique_list_1,unique_list_2,sep="\n")

Output

[10, 20, 30, 40][1, 2, 3, 4, 5]

Time Complexity – O(n)
Space Complexity – O(n)

Python | Get unique values from a list – FAQs

What does distinct() do in Python?

Python itself does not have a built-in distinct() function. However, in the context of databases and some data manipulation libraries like pyspark or SQLAlchemy, distinct() is used to remove duplicate rows from the result set. For example, in pyspark:

from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("example").getOrCreate()
df = spark.createDataFrame([(1, "Alice"), (2, "Bob"), (1, "Alice")], ["id", "name"])
distinct_df = df.distinct()
distinct_df.show()

In native Python, you can achieve distinct elements in a list using a set or list comprehensions.

How do you check if a list contains unique values in Python?

To check if a list contains only unique values, you can compare the length of the list with the length of the set created from the list:

def is_unique(lst):
return len(lst) == len(set(lst))
# Example usage
my_list = [1, 2, 3, 4, 5]
print(is_unique(my_list)) # Output: True

What built-in Python data structure can be used to get unique values from a list?

The set data structure can be used to get unique values from a list:

my_list = [1, 2, 2, 3, 4, 4, 5]
unique_values = list(set(my_list))
print(unique_values) # Output: [1, 2, 3, 4, 5]

How do I count unique items in a list in Python?

You can count the number of unique items in a list by converting the list to a set and then getting the length of the set:

my_list = [1, 2, 2, 3, 4, 4, 5]
unique_count = len(set(my_list))
print(unique_count) # Output: 5

How do I check if a list contains duplicates in Python?

To check if a list contains duplicates, you can compare the length of the list with the length of the set created from the list:

def contains_duplicates(lst):
return len(lst) != len(set(lst))
# Example usage
my_list = [1, 2, 3, 3, 4, 5]
print(contains_duplicates(my_list)) # Output: True


S

Striver

Python | Get unique values from a list - GeeksforGeeks (1)

Improve

Next Article

Python | Select random value from a list

Please Login to comment...

Python | Get unique values from a list - GeeksforGeeks (2024)
Top Articles
Pay-For-Delete Letter: What It Is And How To Write One
Divorce Financial Expert by the Hour
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5722

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.