
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Remove tuples having duplicate first value from given list of tuples in Python
When it is required to remove tuples that have a duplicate first value from a given set of list of tuples, a simple 'for' loop, and the 'add' and 'append' methods can be used.
Below is a demonstration for the same −
Example
my_input = [(45.324, 'Hi Jane, how are you'),(34252.85832, 'Hope you are good'),(45.324, 'You are the best.')] visited_data = set() my_output_list = [] for a, b in my_input: if not a in visited_data: visited_data.add(a) my_output_list.append((a, b)) print("The list of tuple is : ") print(my_input) print("The list of tuple after removing duplicates is :") print(my_output_list)
Output
The list of tuple is : [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good'), (45.324, 'You are the best.')] The list of tuple after removing duplicates is : [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good')]
Explanation
- A list of tuple is defined, and is displayed on the console.
- An empty set is created, as well as an empty list.
- The list of tuple is iterated over, and if it is not present in the 'set', it is added to the set, as well as to the list.
- This is the output that is displayed on the console.
- Related Articles
- Remove duplicate tuples from list of tuples in Python
- Remove tuples from list of tuples if greater than n in Python
- Python - Get sum of tuples having same first value
- Python – Remove Tuples from a List having every element as None
- Remove Tuples from the List having every element as None in Python
- Python | Remove empty tuples from a list
- Python Group tuples in list with same first value
- Find the tuples containing the given element from a list of tuples in Python
- Remove duplicate lists in tuples (Preserving Order) in Python
- Get first element with maximum value in list of tuples in Python
- Combining tuples in list of tuples in Python
- Create a list of tuples from given list having number and its cube in each tuple using Python
- Python – Filter all uppercase characters from given list of tuples
- Count tuples occurrence in list of tuples in Python
- Remove tuple from list of tuples if not containing any character in Python

Advertisements