Python | Even Front digits Test in List
Last Updated :
13 Apr, 2023
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)
Similar Reads
Python - List Elements with given digit
Given list of elements and a digit K, extract all the numbers which contain K digit. Input : test_list = [56, 72, 875, 9, 173], K = 5 Output : [56, 875] Explanation : 56 and 875 has "5" as digit, hence extracted. Input : test_list = [56, 72, 875, 9, 173], K = 4 Output : [] Explanation : No number ha
6 min read
Python - Extract digits from Tuple list
Sometimes, while working with Python lists, we can have a problem in which we need to perform extraction of all the digits from tuple list. This kind of problem can find its application in data domains and day-day programming. Let's discuss certain ways in which this task can be performed. Input : t
6 min read
Python - Test rear digit match in all list elements
Sometimes we may face a problem in which we need to find a list if it contains numbers ending with the same digits. 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 app
6 min read
Python | Test for False list
Sometimes, we need to check if a list is completely True of False, these occurrences come more often in testing purposes after the development phase. Hence, having a knowledge of all this is necessary and useful. Lets discuss certain ways in which this can be performed. Method #1: Naive Method In th
8 min read
Python - Test if string contains element from list
Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string. Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. [GFGTABS] Python s = "Pyth
3 min read
Python - First Even Number in List
Sometimes, while working with lists, we can have a problem in which we need to extract certain numbers occurring first time. This can also be even number. This kind of problem is common in day-day and competitive programming. Lets discuss certain ways in which this task can be performed. Method #1 :
4 min read
Python - Test 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
Python | Check if front digit is Odd in list
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 pro
7 min read
Python - Extract digits from given string
We need to extract the digit from the given string. For example we are given a string s=""abc123def456gh789" we need to extract all the numbers from the string so the output for the given string will become "123456789" In this article we will show different ways to extract digits from a string in Py
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