
- 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 program to extract Mono-digit elements
When it is required to extract mono-digit elements, list comprehension and the ‘all operator are used.
Below is a demonstration of the same −
Example
my_list = [863, 1, 463, "pyt", 782, 241, "is", 639, 4, "fun"] print("The list is :") print(my_list) my_result = [index for index in my_list if all(str(element) == str(index)[0] for element in str(index))] print("The result is :") print(my_result)
Output
The list is : [863, 1, 463, 'pyt', 782, 241, 'is', 639, 4, 'fun'] The result is : [1, 4]
Explanation
A list is defined and displayed on the console.
A list comprehension is used to iterate over the list, and every element is converted to a list and compared with the zeroth element.
This is converted to a list.
This is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Extract tuples having K digit elements in Python
- Python Program to Extract Strings with a digit
- Python program to extract rows with common difference elements
- Python Program to Extract Elements from a List in a Set
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Python – Reform K digit elements
- Python – Extract String elements from Mixed Matrix
- Python – Extract elements from Ranges in List
- Python – Extract tuples with elements in Range
- Python – Extract elements with equal frequency as value
- Program to extract frames using OpenCV in Python?
- Python program to extract Keywords from a list
- Python – Sort by a particular digit count in elements
- Extract Unique dictionary values in Python Program
- Python – Extract elements in between multiple specific range of index

Advertisements