- 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
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 −
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)
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)]
- 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 Articles
- Removing strings from tuple in Python
- Replace duplicates in tuple in Python
- Remove Duplicates from Sorted Array in Python
- Python - Ways to remove duplicates from list
- Remove all duplicates from a given string in Python
- Remove nested records from tuple in Python
- Finding unique elements 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?