# 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))`

Output

```The 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))`

Output

```The 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))`

Output

```The 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))`

Output

```The 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))`

Output

```The 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.`

Output

```The 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.`

Output

```The 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.`

Output

```The 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

1. Initiate a for loop to traverse test_list, variable c=0
2. Check whether each string is numeric or not using regular expression ^[0-9]+\$ and method re.match() if yes increment c by 1
3. If c equals to length of test_list then all strings have digits, set res to True or else False
4. 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))`

Output

```The 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

Previous
Next