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
test_list = [ 25 , 6 , 828829 , 432 ]
print ( "The original list : " + str (test_list))
res = len ( set (( int (sub[ 0 ]) % 2 ) for sub in map ( str , test_list))) = = 1
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*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
test_list = [ 25 , 6 , 828829 , 432 ]
print ( "The original list : " + str (test_list))
res = all ( not int ( str (i)[ 0 ]) % 2 for i in test_list)
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*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
test_list = [ 25 , 6 , 828829 , 432 ]
print ( "The original list : " + str (test_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 ( "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(1)
Method 4: Using filter() + lambda function
Python3
test_list = [ 25 , 6 , 828829 , 432 ]
print ( "The original list : " + str (test_list))
res = len ( list ( filter ( lambda x: int ( str (x)[ 0 ]) % 2 = = 0 , test_list))) = = len (test_list)
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(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
test_list = [ 25 , 6 , 828829 , 432 ]
print ( "The original list : " + str (test_list))
regex_pattern = r '^[02468]+'
res = all (re.match(regex_pattern, str (num)) for num in test_list)
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) 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
import pandas as pd
test_list = [ 25 , 6 , 828829 , 432 ]
print ( "The original list : " + str (test_list))
df = pd.DataFrame(test_list, columns = [ 'numbers' ])
res = df[ 'numbers' ].astype( str ). str [ 0 ].isin([ '0' , '2' , '4' , '6' , '8' ]). all ()
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)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Apr, 2023
Like Article
Save Article