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 problem by converting the elements to the strings and then testing the starting element of the string if they are even we can return true and then convert to set and test for the size of result to be one. The conversion is done by map, set function converts to set and list comprehension checks for first element of string.
Python3
# Python3 code to demonstrate # Even Front digits Test in List # using list comprehension + map() # initializing list test_list = [ 25 , 6 , 828829 , 432 ] # printing original list print ( "The original list : " + str (test_list)) # using list comprehension + map() # Even Front digits Test in List res = len ( set (( int (sub[ 0 ]) % 2 ) for sub in map ( str , test_list))) = = 1 # print result print ( "Does each element start with even digit ? " + str (res)) |
The original list : [25, 6, 828829, 432] Does each element start with even digit ? True
Time Complexity: O(n*n) where n is the number of elements in the test_list. The list comprehension + map() is used to perform the task and it takes O(n*nlogn) time.
Auxiliary Space: O(1) constant additional space is needed.
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 the conversion of string by str function and checking for all elements with the first digit of first element to be even.
Python3
# Python3 code to demonstrate # Even Front digits Test in List # using all() + list comprehension # initializing list test_list = [ 25 , 6 , 828829 , 432 ] # printing original list print ( "The original list : " + str (test_list)) # using all() + list comprehension # Even Front digits Test in List res = all ( not int ( str (i)[ 0 ]) % 2 for i in test_list) # print result print ( "Does each element start with even digit ? " + str (res)) |
The original list : [25, 6, 828829, 432] Does each element start with even digit ? True
Time complexity: O(n*k), where n is the number of elements in the list and k is the number of digits in the largest element.
Auxiliary space: O(1). We only use a constant amount of extra space to store the Boolean result of the all() function.
Method #3: Using for loop
Python3
# Python3 code to demonstrate # Even Front digits Test in List # initializing list test_list = [ 25 , 6 , 828829 , 432 ] # printing original list print ( "The original list : " + str (test_list)) # Even Front digits Test 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 even digit ? " + str (res)) |
The original list : [25, 6, 828829, 432] Does each element start with even digit ? True
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 4: Using filter() + lambda function
Python3
# Python3 code to demonstrate # Even Front digits Test in List # using filter() + lambda function # initializing list test_list = [ 25 , 6 , 828829 , 432 ] # printing original list print ( "The original list : " + str (test_list)) # using filter() + lambda function # Even Front digits Test in List res = len ( list ( filter ( lambda x: int ( str (x)[ 0 ]) % 2 = = 0 , test_list))) = = len (test_list) # print result print ( "Does each element start with even digit ? " + str (res)) |
The original list : [25, 6, 828829, 432] Does each element start with even digit ? True
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach #5: Using a regular expression and match() method:
- Importing the “re” module
- Defining the regular expression pattern
- Applying the regular expression in the list to check if all elements start with even digits
- Printing the result
Python3
import re # initializing list test_list = [ 25 , 6 , 828829 , 432 ] # printing original list print ( "The original list : " + str (test_list)) # using regex # Even Front digits Test in List regex_pattern = r '^[02468]+' res = all (re.match(regex_pattern, str (num)) for num in test_list) # print result print ( "Does each element start with even digit ? " + str (res)) |
The original list : [25, 6, 828829, 432] Does each element start with even digit ? True
Time Complexity: O(N) as we are traversing over the list.
Auxiliary Space: O(1) as we are not using any extra memory.
Method 5: Using pandas module
- Import pandas module.
- Convert the list to a pandas DataFrame.
- Use the str accessor to access the first digit of each element and check if it is even or not.
- Use the all() function to check if all the results are True or not.
- Print the final result.
Python3
# importing pandas module import pandas as pd # initializing list test_list = [ 25 , 6 , 828829 , 432 ] # printing original list print ( "The original list : " + str (test_list)) # using pandas module # Even Front digits Test in List df = pd.DataFrame(test_list, columns = [ 'numbers' ]) res = df[ 'numbers' ].astype( str ). str [ 0 ].isin([ '0' , '2' , '4' , '6' , '8' ]). all () # print result print ( "Does each element start with even digit ? " + str (res)) |
OUTPUT:
The original list : [25, 6, 828829, 432] Does each element start with even digit ? True
Time Complexity: O(n)
Auxiliary Space: O(n)