
- 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 - Check if a List contain particular digits
When it is required to check if a list contains particular digits, the ‘join’ method and a simple iteration are used.
Example
Below is a demonstration of the same
my_list = [427, 789, 345, 122, 471, 124] print("The list is :") print(my_list) my_digits = [1, 4, 7, 2] digit_string = ''.join([str(ele) for ele in my_digits]) all_elems = ''.join([str(ele) for ele in my_list]) my_result = True for element in all_elems: for ele in element: if ele not in digit_string: my_result = False break if(my_result == True): print("The list contains the required digits") else: print("The list doesn't contain the required digits")
Output
The list is : [427, 789, 345, 122, 471, 124] The list doesn't contain the required digits
Explanation
A list is defined and is displayed on the console.
Another list of integers is defined.
A list comprehension is defined to iterate over the list of integers.
The ‘join’ method is used to join the elements.
This is assigned to a variable.
This is done on the original list as well. Let us call it ‘all_elems’.
A variable is assigned to ‘True’/
The ‘all_elems’ list is iterated over, and if the element is not present in the previous list, the variable is assigned ‘False’.
The execution is also broken.
Outside this, if the variable has a value ‘True’, relevant message is defined.
- Related Articles
- Python - Check if list contain particular digits
- How to check if a Python string contains only digits?
- Python Pandas - Check elementwise if the Intervals contain the value
- Python - Check if a list is contained in another list
- Check if all digits of a number divide it in Python
- Python Program for Check if all digits of a number divide it
- Checking if starting digits are similar in list in Python
- Check if a list exists in given list of lists in Python
- Python – Check if particular value is present corresponding to K key
- Python – Average digits count in a List
- Check if a particular key exists in Java LinkedHashMap
- Check if a particular value exists in Java LinkedHashMap
- Check if a particular value exists in Java TreeSet
- Check if a particular element exists in Java LinkedHashSet
- How to check if a list is empty in Python?

Advertisements