- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to extract only the numbers from a list which have some specific digits
When it is required to extract only the numbers from a list which have some specific digits, a list comprehension and the ‘all’ operator is used.
Below is a demonstration of the same −
my_list = [3345, 2345, 1698, 2475, 1932] print("The list is :") print(my_list) digit_list = [2, 3, 5, 4] my_result = [index for index in my_list if all(int(element) in digit_list for element in str(index))] print("The result is :") print(my_result)
The list is : [3345, 2345, 1698, 2475, 1932] The result is : [3345, 2345]
A list is defined and is displayed on the console.
Another list of integers is defined.
The list comprehension is used to iterate over the elements, and the elements are converted to string if they are of integer type.
This is done if all elements are integer type.
It is converted to a list and assigned to a variable.
This is displayed as output on the console.
- Extract digits from Tuple list Python
- Python – Extract Rear K digits from Numbers
- Python program to extract Keywords from a list
- Extract numbers from list of strings in Python
- How to extract only the numbers from a text field in MySQL?
- Python – How to Extract all the digits from a String
- Python Program to Extract Elements from a List in a Set
- Python Extract specific keys from dictionary?
- Program to find elements from list which have occurred at least k times in Python
- Python program to find tuples which have all elements divisible by K from a list of tuples
- How to extract only first sub-element from a list in R?
- Extract csv file specific columns to list in Python
- How to extract numbers from a string in Python?
- How to extract numbers from a string using Python?
- Python program to extract characters in given range from a string list