

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Removing duplicates from tuple in Python
When it is required to remove duplicates from a tuple, the list comprehension is used.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
A list of tuple basically contains tuples enclosed in a list.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
Below is a demonstration of the same −
Example
my_list_1 = [(11, 14), (0, 78), (33, 11), (0, 78)] print("The list of tuple is : ") print(my_list_1) my_unique_list = list(set([i for i in my_list_1])) print("The list of tuples after removing duplicates is :") print(my_unique_list)
Output
The list of tuple is : [(11, 14), (0, 78), (33, 11), (0, 78)] The list of tuples after removing duplicates is : [(33, 11), (11, 14), (0, 78)]
Explanation
- A list of tuple is defined and is displayed on the console.
- The list is iterated through, and is converted to set.
- This way, only unique elements are stored.
- This is again converted to a list.
- This is assigned to a value.
- It is displayed on the console.
- Related Questions & Answers
- Removing strings from tuple in Python
- Replace duplicates in tuple in Python
- Removing adjacent duplicates from a string in JavaScript
- Removing duplicates from a sorted array of literals in JavaScript
- Removing consecutive duplicates from strings in an array using JavaScript
- Removing duplicates and inserting empty strings in JavaScript
- Removing duplicates and keep one instance in JavaScript
- Remove Duplicates from Sorted Array in Python
- Unique sort (removing duplicates and sorting an array) in JavaScript
- Python - Ways to remove duplicates from list
- Remove all duplicates from a given string in Python
- Extract digits from Tuple list Python
- Remove nested records from tuple in Python
- Removing nth character from a string in Python program
- How can I subtract tuple of tuples from a tuple in Python?
Advertisements