
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding Unique Elements from Tuple in Python
In this article, we will learn a Python program to find unique elements from a tuple.
Methods Used
The following are the various methods to accomplish this task ?
Using for loop and append() function
Using collections.Counter() function
Using set() function
Tuples are an immutable, unordered data type used to store collections in Python. It can store multiple values in it. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable.
Method 1: Using for loop and append() function
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. ?
Create a function(uniqueElements) that returns the unique elements in a tuple passed to it as an argument.
Create a new empty list for storing the resultant unique elements of the tuple.
Use the for loop to traverse through each element in a tuple.
Use the if conditional statement along with the not in operator to check whether the corresponding tuple element is not present in the above-defined new list.
Use the append() function(adds the element to the list at the end) to append the element to the new list if the condition is true.
Use the tuple() function to convert the list into a tuple.
Return the resultant tuple.
Create a variable to store the input tuple.
Call the above-defined uniqueElements function by passing an input tuple to it and print the resultant tuple.
Example
The following program returns unique elements in an input tuple using the for loop and append() function ?
# function that returns the unique elements in a tuple passed to it def uniqueElements(inputTuple): # creating a new list for storing unique elements newList = [] # traversing through each element in a tuple for k in inputTuple: # appending that corresponding tuple element to the new list # if it is not present in it(i.e, storing all unique elements) if k not in newList: newList.append(k) # converting the list into a tuple resultTuple = tuple(newList) # returning the resultant tuple return resultTuple # input tuple inputTuple = (5, 1, 8, 7, 7, 3, 3, 6, 1, 6) # Printing input tuple print("The given Tuple is:", inputTuple) #calling the above-defined uniqueElements function by passing input tuple to it print("The Unique elements of the tuple are:", uniqueElements(inputTuple))
Output
On execution, the above program will generate the following output ?
The given Tuple is: (5, 1, 8, 7, 7, 3, 3, 6, 1, 6) The Unique elements of the tuple are: (5, 1, 8, 7, 3, 6)
Method 2: Using collections.Counter() function
Counter() function(a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked)
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. ?
Use the import keyword to import the Counter function from the collections module.
Create a function(uniqueElements) that returns the unique elements in a tuple passed to it as an argument.
Pass the given tuple as an argument to the Counter() function which stores all the unique tuple elements along with their frequencies as a dictionary.
Return the keys(unique tuple elements ) of the above frequency dictionary as a tuple using the keys() and tuple() functions.
Example
The following program returns unique elements in an input tuple using the Counter function of the collections module ?
# importing Counter from the collections module from collections import Counter # function that returns the unique elements in a tuple passed to it def uniqueElements(inputTuple): #Getting all the unique tuple elements along with their frequencies frequency = Counter(inputTuple) # Returning all the unique tuple elements using the keys() function return tuple(frequency.keys()) # input tuple inputTuple = (5, 1, 8, 7, 7, 3, 3, 6, 1, 6) # Printing input tuple print("The given Tuple is:", inputTuple) #calling the above-defined uniqueElements function by passing input tuple to it print("The Unique elements of the tuple are:", uniqueElements(inputTuple))
Output
On execution, the above program will generate the following output ?
The given Tuple is: (5, 1, 8, 7, 7, 3, 3, 6, 1, 6) The Unique elements of the tuple are: (5, 1, 8, 7, 3, 6)
Method 3: Using the set() function
set() function- creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates)
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. ?
Create a function(uniqueElements) that returns the unique elements in a tuple passed to it as an argument.
Return all the unique elements from a tuple using the set() function and convert it into a tuple using the tuple() function(creates a tuple).
Example
The following program returns unique elements in an input tuple using the set() function ?
# function that returns the unique elements in a tuple passed to it def uniqueElements(inputTuple): # Getting all the unique elements of the tuple using the set() function resultUnique = set(inputTuple) # returning unique elements as a tuple using the tuple() function return tuple(resultUnique) # input tuple inputTuple = (5, 1, 8, 'tutorialspoint', 7, 7, 'tutorialspoint', 3, 3, 6, 1, 6) # Printing input tuple print("The given Tuple is:", inputTuple) #calling the above-defined uniqueElements function by passing input tuple to it print("The Unique elements of the tuple are:", uniqueElements(inputTuple))
Output
On execution, the above program will generate the following output ?
The given Tuple is: (5, 1, 8, 'tutorialspoint', 7, 7, 'tutorialspoint', 3, 3, 6, 1, 6) The Unique elements of the tuple are: (1, 3, 5, 6, 7, 8, 'tutorialspoint')
Conclusion
We covered how to find unique elements of a tuple using three ways in this article. We also learned how to use the Counter() function to find not only frequencies but also unique elements in any iterable, such as a list, tuple, etc. The Counter() approach is the most efficient of the three because it uses a dictionary to store frequencies.