Python – Test rear digit match in all list elements
Last Updated :
11 Apr, 2023
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 approach this problem by converting the elements to the strings and then testing the ending element of string and if they are equal we can return true and then convert to set and test for size of result to be one. The conversion is done by map, set function converts to set and list comprehension checks for last element of the string.
Python3
test_list = [ 45 , 545 , 2345 , 8765 ]
print ( "The original list : " + str (test_list))
res = len ( set (sub[ - 1 ] for sub in map ( str , test_list))) = = 1
print ( "Does each element end with same digit ? " + str (res))
|
Output
The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using all() + list comprehension This is yet another approach in which this problem can be solved. In this, we use all functions 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 last digit of the last element.
Python3
test_list = [ 45 , 545 , 2345 , 8765 ]
print ( "The original list : " + str (test_list))
res = all ( str (i)[ - 1 ] = = str (test_list[ - 1 ])[ - 1 ] for i in test_list)
print ( "Does each element end with same digit ? " + str (res))
|
Output
The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3 : Using len() method and * operator
Python3
test_list = [ 45 , 545 , 2345 , 8765 ]
print ( "The original list : " + str (test_list))
res = False
x = []
for i in test_list:
a = str (i)[ - 1 ]
x.append( int (a))
y = [x[ 0 ]] * len (x)
if y = = x:
res = True
print ( "Does each element end with same digit ? " + str (res))
|
Output
The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using filter(),lambda function
Python3
test_list = [ 45 , 545 , 2345 , 8765 ]
print ( "The original list : " + str (test_list))
res = False
last_digit = str (test_list[ 0 ])[ - 1 ]
lis = list ( filter ( lambda x: str (x)[ - 1 ] = = last_digit, test_list))
if ( len (lis) = = len (test_list)):
res = True
print ( "Does each element end with same digit ? " + str (res))
|
Output
The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5: Using Counter() method
Python3
from collections import Counter
test_list = [ 45 , 545 , 2345 , 8765 ]
print ( "The original list : " + str (test_list))
res = False
x = []
for i in test_list:
a = str (i)[ - 1 ]
x.append( int (a))
freq = Counter(x)
if ( len (freq) = = 1 ):
res = True
print ( "Does each element end with same digit ? " + str (res))
|
Output
The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True
Time Complexity: O(N)
Auxiliary Space : O(N)
Method 6: Using itertools.groupby() function:
Python3
import itertools
test_list = [ 45 , 545 , 2345 , 8765 ]
print ( "The original list : " + str (test_list))
res = len ( list (itertools.groupby(test_list, key = lambda x: x % 10 ))) = = 1
print ( "Does each element end with same digit ? " + str (res))
|
Output
The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True
Time Complexity: O(N)
Auxiliary Space : O(N)
Method #7: Using Recursive method.
STEPS:
- Define a function called check_last_digit_match that takes two arguments: lst and last_digit.
- Inside the function, check if the list is empty. If it is, return True since there are no elements to compare.
- Check if last_digit is None. If it is, assign the last digit of the first element of lst to last_digit.
- Check if the last digit of the first element of lst is not equal to last_digit. If it isn’t, return False.
- If the first element of lst passes the last digit check, recursively call the check_last_digit_match function with the remaining elements of lst and last_digit.
- When the recursive call completes, return the result.
- Create a list called test_list and initialize it with four integers.
- Print the original list.
- Call the check_last_digit_match function with the test_list list as the argument and assign the result to a variable called result.
- Print whether each element in the list ends with the same digit by printing Does each element end with same digit? followed by the result variable.
Python3
def check_last_digit_match(lst, last_digit = None ):
if not lst:
return True
if last_digit is None :
last_digit = str (lst[ 0 ])[ - 1 ]
if str (lst[ 0 ])[ - 1 ] ! = last_digit:
return False
return check_last_digit_match(lst[ 1 :], last_digit)
test_list = [ 45 , 545 , 2345 , 8765 ]
print ( "The original list : " + str (test_list))
result = check_last_digit_match(test_list)
print ( "Does each element end with same digit? " , result)
|
Output
The original list : [45, 545, 2345, 8765]
Does each element end with same digit? True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #8 : Using count() and len() methods
Approach
- Initiate a for loop to traverse test_list
- Extract the last digit of each number using str(),int() and append to a new empty list x
- Check whether the count of first element in list x is equal to length of x
- If yes set res to True or else False
Python3
test_list = [ 45 , 545 , 2345 , 8765 ]
print ( "The original list : " + str (test_list))
res = False
x = []
for i in test_list:
a = str (i)[ - 1 ]
x.append( int (a))
res = x.count(x[ 0 ]) = = len (x)
print ( "Does each element end with same digit ? " + str (res))
|
Output
The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True
Time Complexity: O(N)
Auxiliary Space : O(1)
Similar Reads
Python - Match Kth number digit in list elements
Sometimes we may face a problem in which we need to find a list if it contains numbers at the Kth index 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()
7 min read
Test if all elements are present in list-Python
The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.
3 min read
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 - 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 | 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
Remove Negative Elements in List-Python
The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive num
3 min read
Python - Test if any set element exists in List
Given a set and list, the task is to write a python program to check if any set element exists in the list. Examples: Input : test_dict1 = test_set = {6, 4, 2, 7, 9, 1}, test_list = [6, 8, 10] Output : True Explanation : 6 occurs in list from set. Input : test_dict1 = test_set = {16, 4, 2, 7, 9, 1},
4 min read
Python | Test if any list element returns true for condition
Sometimes, while coding in Python, we can have a problem in which we need to filter a list on basis of condition met by any of the element. This can have it's application in web development domain. Let's discuss a shorthand in which this task can be performed. Method : Using any() + list comprehensi
4 min read
Search Elements in a Matrix - Python
The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1
3 min read
Python - Test if all digits starts from % K digit
Sometimes we may face a problem in which we need to find for a list if it contains numbers which are % K. 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 thi
5 min read