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
# Python3 code to demonstrate
# Test rear digit match
# using list comprehension + map()
# initializing list
test_list = [45, 545, 2345, 8765]
# printing original list
print("The original list : " + str(test_list))
# using list comprehension + map()
# Test rear digit match
res = len(set(sub[-1] for sub in map(str, test_list))) == 1
# print result
print("Does each element end with same digit ? " + str(res))
OutputThe 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
# Python3 code to demonstrate
# Test rear digit match
# using all() + list comprehension
# initializing list
test_list = [45, 545, 2345, 8765]
# printing original list
print("The original list : " + str(test_list))
# using all() + list comprehension
# Test rear digit match
res = all(str(i)[-1] == str(test_list[-1])[-1] for i in test_list)
# print result
print("Does each element end with same digit ? " + str(res))
OutputThe 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
# Python3 code to demonstrate
# Test rear digit match
# initializing list
test_list = [45, 545, 2345, 8765]
# printing original list
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 result
print("Does each element end with same digit ? " + str(res))
OutputThe 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
# Python3 code to demonstrate
# Test rear digit match
# initializing list
test_list = [45, 545, 2345, 8765]
# printing original list
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 result
print("Does each element end with same digit ? " + str(res))
OutputThe 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
# Python3 code to demonstrate
# Test rear digit match
from collections import Counter
# initializing list
test_list = [45, 545, 2345, 8765]
# printing original list
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 result
print("Does each element end with same digit ? " + str(res))
OutputThe 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]
# printing original list
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))
#This code is contributed by pinjala Jyothi.
OutputThe 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
# Python3 code to demonstrate
# Test rear digit match
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)
# initializing list
test_list = [45, 545, 2345, 8765]
# printing original list
print("The original list : " + str(test_list))
result = check_last_digit_match(test_list)
# print result
print("Does each element end with same digit? ", result)
#this code contributed by tvsk
OutputThe 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
# Python3 code to demonstrate
# Test rear digit match
# initializing list
test_list = [45, 545, 2345, 8765]
# printing original list
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 result
print("Does each element end with same digit ? " + str(res))
OutputThe 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.U
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 | 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 | 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
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