
- 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
Extract digits from Tuple list Python
When it is required to extract digits from a list of tuple, list comprehension can be used.
Below is the demonstration of the same −
Example
my_list = [(67, 2), (34, 65), (212, 23), (17, 67), (18, )] print("The list is : ") print(my_list) N = 2 print("The value of N is ") print(N) my_result = [sub for sub in my_list if all(len(str(ele)) == N for ele in sub)] print("The extracted tuples are : " ) print(my_result)
Output
The list is : [(67, 2), (34, 65), (212, 23), (17, 67), (18,)] The value of N is 2 The extracted tuples are : [(34, 65), (17, 67), (18,)]
Explanation
A list of tuple is defined, and is displayed on the console.
The value of N is initialized to 2.
This is displayed on the console.
The list comprehension is used to iterate through the list and check if length of all the elements in the list of tuple are equal to a specific value.
If they are equal to a specific value, it is assigned to a variable.
This variable is displayed as output on the console.
- Related Articles
- Python – Extract Rear K digits from Numbers
- Python – Extract Kth element of every Nth tuple in List
- Python program to extract only the numbers from a list which have some specific digits
- Python – Extract elements from Ranges in List
- Python – How to Extract all the digits from a String
- Extract numbers from list of strings in Python
- Python program to extract Keywords from a list
- Python – Extract element from a list succeeded by K
- Find minimum k records from tuple list in Python
- Create a tuple from string and list in Python
- Flatten tuple of List to tuple in Python
- Get maximum of Nth column from tuple list in Python
- Python Program that extract words starting with Vowel From A list
- Python – Extract range of Consecutive similar elements ranges from string list
- Python Program to Extract Elements from a List in a Set

Advertisements