- 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
Python â€“ Filter unique valued tuples
When it is required to filter unique valued tuples from a list of tuples, the ‘list’ and ‘set’ methods are used.
Below is a demonstration of the same −
my_list = [(42, 51), (46, 71), (14, 25), (26, 91), (56, 0), (11, 1), (99,102)] print("The list of tuple is :") print(my_list) my_list.sort() print("The list after sorting is :") print(my_list) my_result = list(set(my_list)) print("The result is :") print(my_result)
The list of tuple is : [(42, 51), (46, 71), (14, 25), (26, 91), (56, 0), (11, 1), (99, 102)] The list after sorting is : [(11, 1), (14, 25), (26, 91), (42, 51), (46, 71), (56, 0), (99, 102)] The result is : [(11, 1), (26, 91), (42, 51), (46, 71), (56, 0), (14, 25), (99, 102)]
A list of tuple of integers is defined and is displayed on the console.
It is sorted using the ‘sort’ method.
This is displayed on the console.
It is converted to a set to get the unique values, and then to a list.
This result is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Python â€“ Filter Tuples with Integers
- Python â€“ Filter consecutive elements Tuples
- Python â€“ Filter Tuples Product greater than K
- Python â€“ Filter Tuples with Strings of specific characters
- Filter Tuples by Kth element from List in Python
- Filter tuples according to list element presence in Python
- Python â€“ Filter all uppercase characters from given list of tuples
- How to split Python tuples into sub-tuples?
- Combining tuples in list of tuples in Python
- Count tuples occurrence in list of tuples in Python
- How to Concatenate tuples to nested tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Filter in Python
- Updating Tuples in Python