Python Program to check whether all elements in a string list are numeric
Last Updated :
22 Apr, 2023
Given a list that contains only string elements the task here is to write a Python program to check if all of them are numeric or not. If all are numeric return True otherwise, return False.
Input : test_list = ["434", "823", "98", "74"]
Output : True
Explanation : All Strings are digits.
Input : test_list = ["434", "82e", "98", "74"]
Output : False
Explanation : e is not digit, hence verdict is False.
Method 1 : Using all(), isdigit() and generator expression
In this, we check for numbers from isdigit(). all() is used to check for all strings to be number, and iteration for each string is done using generator expression.
Example:
Python3
# initializing list
test_list = ["434", "823", "98", "74"]
# printing original list
print("The original list is : " + str(test_list))
# checking all elements to be numeric using isdigit()
res = all(ele.isdigit() for ele in test_list)
# printing result
print("Are all strings digits ? : " + str(res))
OutputThe original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using all(), isdigit() and map()
In this, we extend test logic to each string using map(), rather than generator expression. Rest all the functionalities are performed similarly to the above method.
Example:
Python3
# initializing list
test_list = ["434", "823", "98", "74"]
# printing original list
print("The original list is : " + str(test_list))
# checking all elements to be numeric using isdigit()
# map() to extend to each element
res = all(map(str.isdigit, test_list))
# printing result
print("Are all strings digits ? : " + str(res))
OutputThe original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 : Using isnumeric() and len() methods
Python3
# initializing list
test_list = ["434", "82e", "98", "74"]
# printing original list
print("The original list is : " + str(test_list))
# checking all elements to be numeric using isdigit()
c=0
for i in test_list:
if i.isnumeric():
c+=1
res=False
if(c==len(test_list)):
res=True
# printing result
print("Are all strings digits ? : " + str(res))
OutputThe original list is : ['434', '82e', '98', '74']
Are all strings digits ? : False
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4 : Using replace() and len() methods
Python3
# Python Program to check whether all elements in a string list are numeric
# initializing list
test_list = ["434", "82e", "98", "74"]
# printing original list
print("The original list is : " + str(test_list))
# checking all elements to be numeric using isdigit()
c = 0
numbers = "0123456789"
for i in test_list:
for j in numbers:
i = i.replace(j, "")
if(len(i) == 0):
c += 1
res = False
if(c == len(test_list)):
res = True
# printing result
print("Are all strings digits ? : " + str(res))
OutputThe original list is : ['434', '82e', '98', '74']
Are all strings digits ? : False
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 5 : Using filter()+list()+lambda functions
Python3
# initializing list
test_list = ["434", "823", "98", "74"]
# printing original list
print("The original list is : " + str(test_list))
# checking all elements to be numeric using isdigit()
res = list(filter(lambda x: x.isdigit(), test_list))
if(len(res) == len(test_list)):
res = True
else:
res = False
# printing result
print("Are all strings digits ? : " + str(res))
OutputThe original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 6 : Using reduce()
Python3
from functools import reduce
from operator import and_
test_list = ["434", "823", "98", "74"]
print("The original list is : " + str(test_list))
res = reduce(and_, (string.isdigit() for string in test_list))
print("Are all strings digits ? : " + str(res))
#This code is contributed by Jyothi pinjala.
OutputThe original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 7:Using for loop
Python3
test_list = ["434", "823", "98", "74"]
print("The original list is : " + str(test_list))
for string in test_list:
if not string.isdigit():
res = False
break
else:
res = True
print("Are all strings digits ? : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time complexity: O(n)
Auxiliary Space: O(1)
Method 8: Using loop +try and except method.
Python3
test_list = ["434", "823", "98", "74"]
print("The original list is : " + str(test_list))
for i in test_list:
try:
int(i)
res=True
except:
res=False
print("Are all strings digits ? : " + str(res))
#This code is contributed by tvsk.
OutputThe original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time complexity: O(n)
Auxiliary Space: O(1)
Method 9: Using re.match() method
Approach
- Initiate a for loop to traverse test_list, variable c=0
- Check whether each string is numeric or not using regular expression ^[0-9]+$ and method re.match() if yes increment c by 1
- If c equals to length of test_list then all strings have digits, set res to True or else False
- Display res
Python3
import re
# initializing list
test_list = ["434", "823", "98", "74"]
# printing original list
print("The original list is : " + str(test_list))
# checking all elements to be
# numeric using isdigit()
c = 0
for i in test_list:
if re.match('^[0-9]+$', i):
c += 1
res = c == len(test_list)
# printing result
print("Are all strings digits ? : " + str(res))
OutputThe original list is : ['434', '823', '98', '74']
Are all strings digits ? : True
Time Complexity: O(N) N - length of test_list
Auxiliary Space: O(1) because we used single variable res to store result
Similar Reads
Python Program to check whether Characters of all string elements are in lexical order or not Given a list with string elements, the task is to write a Python program to return True if all list elements are sorted. The point of caution here is to keep in mind we are comparing characters of a string list element with other characters of the same list element and not string elements with each
4 min read
Python program to check if a string has at least one letter and one number The task is to verify whether a given string contains both at least one letter (either uppercase or lowercase) and at least one number. For example, if the input string is "Hello123", the program should return True since it contains both letters and numbers. On the other hand, a string like "Hello"
3 min read
Python program to Convert a elements in a list of Tuples to Float Given a Tuple list, convert all possible convertible elements to float. Input : test_list = [("3", "Gfg"), ("1", "26.45")] Output : [(3.0, 'Gfg'), (1.0, 26.45)] Explanation : Numerical strings converted to floats. Input : test_list = [("3", "Gfg")] Output : [(3.0, 'Gfg')] Explanation : Numerical str
5 min read
Python program to calculate the number of digits and letters in a string In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Check Whether String Contains Only Numbers or Not - Python We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains
3 min read
Python Program to test if the String only Numbers and Alphabets Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Expl
4 min read
Python program to list Sort by Number value in String Given a List of strings, the task is to write a Python program to sort list by the number present in the Strings. If no number is present, they will be taken to the front of the list. Input : test_list = ["gfg is 4", "all no 1", "geeks over 7 seas", "and 100 planets"] Output : ['all no 1', 'gfg is 4
6 min read
Python - Test if all elements in list are of same type When working with lists in Python, there are times when we need to ensure that all the elements in the list are of the same type or not. This is particularly useful in tasks like data analysis where uniformity is required for calculations. In this article, we will check several methods to perform th
2 min read
Python regex | Check whether the input is Floating point number or not Prerequisite: Regular expression in Python Given an input, write a Python program to check whether the given Input is Floating point number or not. Examples: Input: 1.20 Output: Floating point number Input: -2.356 Output: Floating point number Input: 0.2 Output: Floating point number Input: -3 Outpu
2 min read