Python | Check if front digit is Odd in list
Last Updated :
08 May, 2023
Sometimes we may face a problem in which we need to find a list if it contains numbers that are odd. This particular utility has an application in day-day programming. Let’s discuss certain ways in which this task can be achieved.
Method #1 : Using list comprehension + map()
We can approach this problem by converting the elements to strings and then testing the starting element of the string if they are odd we can return true and then convert them to set and test for the size of the result to be one. The conversion is done by map, set function converts to set and list comprehension checks for the first element of the string.
Python3
# Python3 code to demonstrate
# Check if front digit is Odd in list
# using list comprehension + map()
# initializing list
test_list = [15, 7, 928829, 332]
# printing original list
print("The original list : " + str(test_list))
# using list comprehension + map()
# Check if front digit is Odd in list
res = len(set( not(int(sub[0]) % 2) for sub in map(str, test_list))) == 1
# print result
print("Does each element start with odd digit ? " + str(res))
OutputThe original list : [15, 7, 928829, 332]
Does each element start with odd digit ? True
Time complexity: O(n), where n is the number of elements in the list. This is because we are using the map and set functions, which take O(n) time each, and also doing a constant amount of work within the loop.
Auxiliary space: O(n), as we are storing the results in a set.
Method #2 : Using all() + list comprehension
This is yet another approach in which this problem can be solved. In this we use all function to check for all elements and return a Boolean result and list comprehension does the part of conversion of string by str function and checking for all elements with the first digit of first element to be odd.
Python3
# Python3 code to demonstrate
# Check if front digit is Odd in list
# using all() + list comprehension
# initializing list
test_list = [15, 7, 928829, 332]
# printing original list
print("The original list : " + str(test_list))
# using all() + list comprehension
# Check if front digit is Odd in list
res = all(int(str(i)[0]) % 2 for i in test_list)
# print result
print("Does each element start with odd digit ? " + str(res))
OutputThe original list : [15, 7, 928829, 332]
Does each element start with odd digit ? True
Time Complexity: O(n), where n is the number of elements in the list "test_list".
Auxiliary Space: O(1), as no additional data structures are used and only a few variables are used.
Method #3 : Using for loop
Python3
# Python3 code to demonstrate
# Check if front digit is Odd in list
# initializing list
test_list = [15, 7, 928829, 332]
# printing original list
print("The original list : " + str(test_list))
# Check if front digit is Odd in list
res=False
c=0
for i in test_list:
a=str(i)
if(int(a[0])%2!=0):
c+=1
if(c==len(test_list)):
res=True
# print result
print("Does each element start with odd digit ? " + str(res))
OutputThe original list : [15, 7, 928829, 332]
Does each element start with odd digit ? True
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 4: Using a lambda function and filter() built-in function
You can use a lambda function with filter() function to filter out the elements in the list that do not start with an odd digit. The resulting list will be empty if all elements start with odd digits, else it will contain some elements. Then you can check if the length of the resulting list is 0 to determine if all elements start with odd digits or not.
- Define a list called test_list with some elements.
- Print the original list using the print() function and string concatenation.
- Create a lambda function that takes an argument x and returns True if the first digit of the string representation of x is not one of the odd digits (1, 3, 5, 7, 9), else it returns False.
- Use the filter() function with the lambda function and the test_list as arguments to create a new list that contains only the elements of test_list that do not start with an odd digit.
- Convert the filtered list into a regular list using the list() function.
- Calculate the length of the resulting list using the len() function.
- Check if the length of the resulting list is equal to zero, which indicates that all elements in test_list start with an odd digit.
- Assign the resulting boolean value to the variable res.
- Print the final result using the print() function and string concatenation.
Python3
# Python3 code to demonstrate
# Check if front digit is Odd in list
# initializing list
test_list = [15, 7, 928829, 332]
# printing original list
print("The original list : " + str(test_list))
# Check if front digit is Odd in list
res = len(list(filter(lambda x: str(x)[0] not in ['1', '3', '5', '7', '9'], test_list))) == 0
# print result
print("Does each element start with odd digit ? " + str(res))
OutputThe original list : [15, 7, 928829, 332]
Does each element start with odd digit ? True
Time Complexity: O(n) as it needs to traverse the list once.
Auxiliary Space: O(k), where k is the number of elements that do not start with an odd digit. However, the maximum value of k can be n, so the worst-case space complexity is O(n).
Method #5: Using a generator expression and all() built-in function
In this approach, we will use a generator expression to check if the first digit of each element in the list is odd or not. Then, we will use the all() built-in function to check if all the elements in the generator expression are True or not. If all the elements are True, then we will set the result to True; otherwise, we will set the result to False.
Approach:
- Initialize the test_list.
- Define a generator expression that checks if the first digit of each element in the list is odd or not.
- Use the all() built-in function to check if all the elements in the generator expression are True or not.
- Set the result to True if all the elements are True; otherwise, set the result to False.
- Print the result.
Python3
# Python3 code to demonstrate
# Check if front digit is Odd in list
# initializing list
test_list = [15, 7, 928829, 332]
# printing original list
print("The original list : " + str(test_list))
# Check if front digit is Odd in list using generator expression and
# all() built-in function
result = all(int(str(i)[0]) % 2 != 0 for i in test_list)
# print result
print("Does each element start with odd digit ? " + str(result))
OutputThe original list : [15, 7, 928829, 332]
Does each element start with odd digit ? True
Time complexity: O(n), where n is the length of the list, because we need to iterate over all the elements in the list once to check the first digit of each element.
Auxiliary space: O(1), because we are not using any additional space to store the intermediate results.
Similar Reads
Python | Even Front digits Test in List Sometimes we may face a problem in which we need to find a list if it contains numbers that are even. This particular utility has an application in day-day programming. Letâs discuss certain ways in which this task can be achieved. Method #1: Using list comprehension + map() We can approach this pro
5 min read
Python | Check if any String is empty in list Sometimes, while working with Python, we can have a problem in which we need to check for perfection of data in list. One of parameter can be that each element in list is non-empty. Let's discuss if a list is perfect on this factor using certain methods. Method #1 : Using any() + len() The combinati
6 min read
Python - Test if Kth character is digit in String Given a String, check if Kth index is a digit. Input : test_str = 'geeks9geeks', K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str = 'geeks9geeks', K = 4 Output : False Explanation : 4th idx element is s, not a digit, hence False. Method #1: Using in operat
5 min read
Python - Check if list contain particular digits Given a List and some digits, the task is to write a python program to check if the list contains only certain digits. Input : test_list = [435, 133, 113, 451, 134], digs = [1, 4, 5, 3] Output : True Explanation : All elements are made out of 1, 4, 5 or 3 only.Input : test_list = [435, 133, 113, 451
14 min read
Check if any element in list satisfies a condition-Python The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio
2 min read
Python - Get Indices of Even Elements from list Sometimes, while working with Python lists, we can have a problem in which we wish to find Even elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Let us discuss certain ways to find in
8 min read