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
How to Check if an Index Exists in Python Lists
When working with lists in Python, sometimes we need to check if a particular index exists. This is important because if we try to access an index that is out of range, we will get an error. Let's look at some simple ways to check if an index exists in a Python list.The easiest methods to check if g
2 min read
Python | Check for Nth index existence in list
Sometimes, while working with lists, we can have a problem in which we require to insert a particular element at an index. But, before that it is essential to know that particular index is part of list or not. Let's discuss certain shorthands that can perform this task error free. Method #1 : Using
3 min read
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 | Check If A Given Object Is A List Or Not
Given an object, the task is to check whether the object is a list or not. Python provides few simple methods to do this and in this article, we'll walk through the different ways to check if an object is a list:Using isinstance()isinstance() function checks if an object is an instance of a specific
2 min read
Python | Check if all elements in list follow a condition
Sometimes, while working with Python list, we can have a problem in which we need to check if all the elements in list abide to a particular condition. This can have application in filtering in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using
4 min read
Python - Check if element is present in tuple
We are given a tuple and our task is to find whether given element is present in tuple or not. For example x = (1, 2, 3, 4, 5) and we need to find if 3 is present in tuple so that in this case resultant output should be True.Using in Operatorin operator checks if an element is present in a tuple by
2 min read
Python - Convert Float to digit list
We are given a floating-point number and our task is to convert it into a list of its individual digits, ignoring the decimal point. For example, if the input is 45.67, the output should be [4, 5, 6, 7].Using string manipulationIn this method, the number is converted to a string and then each charac
4 min read
Python - Check if List contains elements in Range
Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range.Using any() Function -
3 min read
How to Check End of For Loop in Python
In Python, we often use loops to iterate over a collection like list or string. But sometimes, we may want to know when the loop has reached its end. There are different ways to check for the end of a for loop. Let's go through some simple methods to do this.Using else with For Loop (Most Efficient)
2 min read