Open In App

Python | Check if front digit is Odd in list

Improve
Improve
Like Article
Like
Save
Share
Report

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


Output

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


Output

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


Output

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

  1. Define a list called test_list with some elements.
  2. Print the original list using the print() function and string concatenation.
  3. 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.
  4. 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.
  5. Convert the filtered list into a regular list using the list() function.
  6. Calculate the length of the resulting list using the len() function.
  7. 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.
  8. Assign the resulting boolean value to the variable res.
  9. 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))


Output

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

  1. Initialize the test_list.
  2. Define a generator expression that checks if the first digit of each element in the list is odd or not.
  3. Use the all() built-in function to check if all the elements in the generator expression are True or not.
  4. Set the result to True if all the elements are True; otherwise, set the result to False.
  5. 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))


Output

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



Last Updated : 08 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads