Open In App

Python – Match Kth number digit in list elements

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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() 

We can approach this problem by converting the elements to the strings and then testing the Kth element of the string and if they are equal we can return true and then convert to set and test for the size of the result to be one. The conversion is done by map, the set function converts to set and list comprehension checks for the Kth element of the string.

Python3




# Python3 code to demonstrate
# Kth Number digit match
# using list comprehension + map()
 
# initializing list
test_list = [467, 565, 2645, 8668]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 1
 
# using list comprehension + map()
# Kth Number digit match
res = len(set(sub[K] for sub in map(str, test_list))) == 1
 
# Printing the  result
print("Does each element of index K have same digit ? " + str(res))


Output : 

The original list : [467, 565, 2645, 8668]
Does each element of index K have same digit ? True

Time complexity: O(n), where n is the length of the list ‘test_list’.
Auxiliary space: O(n), where n is the length of the list ‘test_list’.

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 Kth digit of Kth element. 

Python3




# Python3 code to demonstrate
# Kth Number digit match
# using list comprehension + all()
 
# initializing list
test_list = [467, 565, 2645, 8668]
 
# Printing original list
print(& quot
       The original list : & quot
       + str(test_list))
 
# Initializing K
K = 1
 
# using list comprehension + all() method
# Kth Number digit match
res = all(str(i)[K] == str(test_list[K])[K] for i in test_list)
 
# print result
print(& quot
       Does each element of index K have same digit ? & quot
       + str(res))


Output : 

The original list : [467, 565, 2645, 8668]
Does each element of index K have same digit ? True

Time complexity: O(n), where n is the length of the test_list. 
Auxiliary Space: O(1), constant extra space is required.

Method #3: Using for loop and len() method

Python3




# Python3 code to demonstrate
# Kth Number digit match
 
# initializing list
test_list = [467, 565, 2645, 8668]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 1
res = False
x = []
for i in test_list:
    a = str(i)
    x.append(int(a[K]))
y = [x[0]]*len(x)
if(y == x):
    res = True
# print result
print("Does each element of index K have same digit ? " + str(res))


Output

The original list : [467, 565, 2645, 8668]
Does each element of index K have same digit ? True

Time Complexity: O(n), where n is the length of the input list. This is because we’re using loop + len() method which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space 

Method #4 : Using reduce() and lambda

Python3




# Python3 code to demonstrate
# Kth Number digit match
# using reduce() and lambda
 
# Importing reduce module
from functools import reduce
 
# initializing list
test_list = [467, 565, 2645, 8668]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Initializing K
K = 1
 
# using reduce() and lambda
# Kth Number digit match
res = reduce(lambda x, y: x and (str(y)[K] == str(test_list[K])[K]), test_list)
 
# Printing result
print("Does each element of index K have same digit ? " + str(res))
 
# This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [467, 565, 2645, 8668]
Does each element of index K have same digit ? True

This method uses the reduce() function with a lambda function to check if all the Kth digits of the elements in the input list are the same. The lambda function compares the Kth digit of each element to the Kth digit of the first element in the input list.

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #5: Using Recursion

Step-by-step approach:

  • Define a function `check_kth_digit()` that takes `test_list` and `K` as inputs.
  • Check if the length of `test_list` is 0, if yes, return True as all elements have the same digit at index K.
  • Otherwise, convert the first element of `test_list` to a string and extract the Kth digit.
  • Loop through all other elements in `test_list` and compare their Kth digit with the extracted Kth digit from the first element.
  • If any mismatch is found, return False as not all elements have the same digit at index K.
  • If no mismatch is found, call `check_kth_digit()` recursively on the remaining elements of `test_list` (excluding the first element).
  • Return the result of the recursive call.

Python3




def check_kth_digit(test_list, K):
 
    if len(test_list) == 0# base case
        return True
    else:
       
        a = str(test_list[0])
        x = int(a[K])
        for i in test_list:
            a = str(i)
            if int(a[K]) != x:
                return False
               
        # Recursive call
        return True and check_kth_digit(test_list[1:], K)
 
 
# Initializing list
test_list = [467, 565, 2645, 8668]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Initializing K
K = 1
 
# Checking if each element of index K has the same digit
res = check_kth_digit(test_list, K)
 
# Printing the result
print("Does each element of index K have the same digit? " + str(res))


Output

The original list : [467, 565, 2645, 8668]
Does each element of index K have the same digit? True

Time complexity: O(N*M), where N is the number of elements in `test_list` and M is the maximum number of digits in an element of `test_list`. This is because we need to loop through each element of `test_list` and extract the Kth digit, which takes O(M) time, and we do this for N elements.

Auxiliary Space: O(M), as we are storing the extracted Kth digit in a variable. Note that the space complexity could be higher if the input elements in `test_list` are very large and require a significant amount of memory for string conversion and storage of digits.

Method #6: Using a set

Step-by-step approach:

  • Initialize a set named “digits” to store the digits present in the Kth position of each element of the list.
  • Loop through the elements of the list.
  • Convert the current element to a string and extract the Kth digit using string indexing.
  • Add the extracted digit to the “digits” set.
  • Check if the length of the “digits” set is 1. If yes, return True, else return False.

Python3




def check_kth_digit(test_list, K):
    digits = set()
    for num in test_list:
        digit = str(num)[K]
        digits.add(digit)
    return len(digits) == 1
 
 
# Initializing list
test_list = [467, 565, 2645, 8668]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Initializing K
K = 1
 
# Checking if each element of index K has the same digit
res = check_kth_digit(test_list, K)
 
# Printing the result
print("Does each element of index K have the same digit? " + str(res))


Output

The original list : [467, 565, 2645, 8668]
Does each element of index K have the same digit? True

Time complexity: O(n*k), where n is the length of the list and k is the number of digits in each element.
Auxiliary space: O(k), for storing the digits in the set.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads