Python Program to check whether all elements in a string list are numeric


When it is required to check wether all elements in a list of strings are numeric, the ‘all’ operator is used.

Example

Below is a demonstration of the same

my_list = ["434", "823", "98", "74", '9870']

print("The list is :")
print(my_list)

my_result = all(ele.isdigit() for ele in my_list)

if(my_result == True):
   print("All the elements in the list are numeric")
else:
   print("All the elements in the list are not numeric")

Output

The list is :
['434', '823', '98', '74', '9870']
All the elements in the list are numeric

Explanation

  • A list of integers is defined and is displayed on the console.

  • The 'all' operator is used to check if every element is a digit or not.

  • This is done using the 'isdigit' method.

  • The result of this operation is assigned to a variable.

  • Based on the Boolean value of the result, the relevant message is displayed on the console.

Updated on: 16-Sep-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements