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 whether number formed by combining all elements of the array is palindrome
Given an array arr[], the task is to combine all the elements in the array sequentially and check if it is a palindrome. Examples: Input: arr[] ={1 , 69 , 54 , 45 , 96 , 1} Output: palindrome Explanation: The number formed by combining all the elements is "1695445961" which is a palindrome Input: ar
4 min read
Python Program To Check If A Linked List Of Strings Forms A Palindrome
Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco
2 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
Python - Check if all elements in List are same
To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
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 check if given string is pangram
The task is to check if a string is a pangram which means it includes every letter of the English alphabet at least once. In this article, weâll look at different ways to check if the string contains all 26 letters.Using Bitmasking Bitmasking uses a number where each bit represents a letter in the a
2 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