Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Tuple elements inversions
Next article icon

Find Unique Elements from Tuple in Python

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Tuples are immutable built-in data type in Python that can store multiple values in it. Extracting Unique Elements from a Tuple in Python can be done through two different approaches.

Examples:

Input: (1, 2, 13, 4, 3, 12, 5, 7, 7, 2, 2, 4)
Output: (1, 2, 3,4,5,12,13)Input: ('Apple', 'Mango', 'Banana', 'Mango', 'Apple')
Output: ('Apple', 'Mango', 'Banana')

Let's start with the different methods :

By using brute force to get unique elements from tuples

In brute force, we will be using 2 for loops for checking the same values.

Python3
def unique(numbers):     for i in range(len(numbers)):         for j in range(i+1, len(numbers)):             if numbers[i] == numbers[j]:                 return i   # print the unique tuple by extracting all the unique elements numbers = (1, 2, 3, 4, 2, 2, 2, 1, 5, 4, 3, 4, 3) print(unique_numbers(numbers)) 

Output:

(1, 2, 3, 4, 5)

Time Complexity: O(n^2) for two loops
Auxiliary Space: O(1) as no extra space used

By iterative method to get unique elements from tuples

In this method, a loop can be used to store the unique values in a list and then converting that list into tuple.

Python3
# function for iteration and finding unique values def unique_numbers(numbers):     un = []     for num in numbers:         if num not in un:             un.append(num)     # to convert list into tuple using tuple() function     unique_tuple = tuple(un)     return unique_tuple   # print the unique tuple by extracting all the unique elements numbers = (1, 2, 3, 4, 2, 2, 2, 1, 5, 4, 3, 4, 3) print(unique_numbers(numbers)) 

Output
(1, 2, 3, 4, 5) 

The time complexity of this function is O(n^2), because the "in" operator in the if statement has a time complexity of O(n) and it's used n times in the for loop.

The space complexity is O(n), because the maximum space required by the un list is n (when all elements in the input numbers are unique).

By using set data-structure to get unique elements from tuples

As set stores unique values so we use a set to get the unique values from a tuple.

Python3
# To find the unique elements from the tuple using the set def unique_numbers(numbers):     # this will take only unique numbers from the tuple     return tuple(set(numbers))   numbers = (1, 2, 3, 4, 'hello', 2, 5, 7, 'hello', 7, 2, 2, 4) print(unique_numbers(numbers)) 

Output
(1, 2, 3, 4, 5, 7, 'hello') 

The time complexity of this function is O(n), because the set() function has a time complexity of O(n) to convert an iterable object into a set object.

The space complexity is also O(n), because the maximum space required by the set object is n (when all elements in the input numbers are unique).

Find Unique Elements from Tuple Using Counter() function

Python3
from collections import Counter # To find the unique elements from the tuple using the counter   def unique_numbers(numbers):     # this will take only unique numbers from the tuple     return tuple(Counter(numbers).keys())   numbers = (1, 2, 3, 4, 2, 5, 7, 2, 2, 4) print(unique_numbers(numbers)) 

Output
(1, 2, 3, 4, 5, 7) 

The time complexity of this function is O(n), because the Counter() function from the collections library has a time complexity of O(n) to count the occurrences of elements in an iterable object.

The space complexity is also O(n), because the maximum space required by the Counter object is n (when all elements in the input numbers are unique).

Find Unique Elements from Tuple Using re module.

The program uses the re module in Python to extract unique elements from a given tuple. It converts the tuple to a string and uses the re.findall() method to extract all the digits from the string as strings. It then uses the map() function to convert the strings to integers and then converts the resulting list to a set to remove duplicates. Finally, it converts the set back to a tuple and prints the result

Python3
import re  numbers = (1, 2, 3, 4, 2, 5, 7, 2, 2, 4) unique_numbers = tuple(map(int, re.findall(r'\d+', str(numbers)))) unique_numbers = tuple(set(unique_numbers)) print(unique_numbers) 

Output
(1, 2, 3, 4, 5, 7) 

Time complexity:
The time complexity of the program is O(n) where n is the number of elements in the input tuple. The re.findall() method has a time complexity of O(n) where n is the length of the string, and the set() function has a time complexity of O(n) where n is the number of elements in the input iterable.

Auxiliary Space:
The space complexity of the program is also O(n) where n is the number of elements in the input tuple. This is because the program creates a new list to store the extracted digits as strings, and then creates a new set to remove duplicates before converting back to a tuple. The space used by these data structures is proportional to the number of elements in the input tuple.

Find Unique Elements from Tuple Using Enumeration()

Python
# function for finding unique values def unique_numbers(numbers):     un = tuple(j for i, j in enumerate(numbers) if numbers.index(j) == i)     return un   # print the unique tuple by extracting all the unique elements numbers = (1, 2, 3, 4, 2, 2, 2, 1, 5, 4, 3, 4, 3) print(unique_numbers(numbers)) 

Output:

(1, 2, 3, 4, 5)

Time Complexity: O(N) Where N is the length of the Test tuple. 
Auxiliary Space: O(M) Where M is the length of the new Tuple.  


Next Article
Python - Tuple elements inversions

K

krishnav4
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Python Programs
  • Technical Scripter 2022
Practice Tags :
  • python

Similar Reads

  • Python | How to get unique elements in nested tuple
    Sometimes, while working with tuples, we can have a problem in which we have nested tuples and we need to extract elements that occur singly, i.e are elementary. This kind of problem can have applications in many domains. Let's discuss certain ways in which this problem can be solved. Method #1: Usi
    7 min read
  • Python - Tuple elements inversions
    Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among tuple elements. This is an essential utility as we come across bitwise operations many times. Let’s discuss certain ways in which this task can be performed. Method #1 : Using map() + l
    3 min read
  • Python - Elements frequency in Tuple
    Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
    7 min read
  • Python | Find Dissimilar Elements in Tuples
    Sometimes, while working with tuples, we can have a problem in which we need dissimilar features of two records. This type of application can come in Data Science domain. Let's discuss certain ways in which this problem can be solved. Method #1 : Using set() + "^" operator This task can be performed
    8 min read
  • Python | N element incremental tuples
    Sometimes, while working with data, we can have a problem in which we require to gather a data that is of the form of sequence of increasing element tuple with each tuple containing the element N times. Let's discuss certain ways in which this task can be performed. Method #1 : Using generator expre
    5 min read
  • Python - Filter Tuples by Kth element from List
    Given a list of tuples, filter by Kth element presence in List. Input : test_list = [("GFg", 5, 9), ("is", 4, 3), ("best", 10, 29)], check_list = [4, 2, 3, 10], K = 2 Output : [('is', 4, 3)] Explanation : 3 is 2nd element and present in list, hence filtered tuple. Input : test_list = [("GFg", 5, 9),
    5 min read
  • Python | Get unique tuples from list
    Sometimes, while working with Python list, we can come across a problem in which we require to find the unique occurrences of list. Having elementary data types is easy to handle, but sometime, we might have complex data types and the problem becomes new in that cases. Let's discuss certain ways in
    3 min read
  • Python - Modulo of tuple elements
    Sometimes, while working with records, we can have a problem in which we may need to perform a modulo of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of the above f
    8 min read
  • Update Each Element in Tuple List - Python
    The task of updating each element in a tuple list in Python involves adding a specific value to every element within each tuple in a list of tuples. This is commonly needed when adjusting numerical data stored in a structured format like tuples inside lists. For example, given a = [(1, 3, 4), (2, 4,
    4 min read
  • Get first element from a List of tuples - Python
    The goal here is to extract the first element from each tuple in a list of tuples. For example, given a list [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby')], we want to retrieve [1, 2, 3], which represents the first element of each tuple. There are several ways to achieve this, each varying in terms of
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences