Open In App

Python – Extract Rear K digits from Numbers

Given an Integer list, extract rear K digits from it.

Input : test_list = [5645, 23567, 34543, 87652, 2342], K = 2 
Output : [45, 67, 43, 52, 42] 
Explanation : Last 2 digits extracted.



Input : test_list = [5645, 23567, 34543, 87652, 2342], K = 4 
Output : [5645, 3567, 4543, 7652, 2342] 
Explanation : Last 4 digits extracted. 
 

Method #1 : Using list comprehension + % operator



In this technique, we modulo each number with 10^K to get the desired last K digits of each number.




# Python3 code to demonstrate working of
# Extract Rear K digits from Numbers
# Using list comprehension + % operator
 
# initializing list
test_list = [5645, 23567, 34543, 87652, 2342]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# Getting remainder for each element
res = [ele % (10 ** K) for ele in test_list]
 
# printing result
print("Rear K digits of elements ? : " + str(res))

Output
The original list is : [5645, 23567, 34543, 87652, 2342]
Rear K digits of elements ? : [645, 567, 543, 652, 342]

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

Method #2 : Using str() + slicing 

In this, we perform task of getting rear elements using list slicing, and str() is used to convert each element to string.




# Python3 code to demonstrate working of
# Extract Rear K digits from Numbers
# Using str() + slicing
 
# initializing list
test_list = [5645, 23567, 34543, 87652, 2342]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# getting integer using int() after slicing string
res = [int(str(idx)[-K:]) for idx in test_list]
 
# printing result
print("Rear K digits of elements ? : " + str(res))

Output
The original list is : [5645, 23567, 34543, 87652, 2342]
Rear K digits of elements ? : [645, 567, 543, 652, 342]

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

Method #3 : Using map() function and lambda expression: 

Algorithm:

1.Initialize a list, test_list, with some integers.
2.Initialize an integer, K, representing the number of digits to extract from the rear of each element in the list.
3.Apply the modulo operator to each element in the test_list with K.
4.Store the result of the above operation in a new list, res.
5.Return the res list.




# initializing list
test_list = [5645, 23567, 34543, 87652, 2342]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using map() function and lambda expression to extract last K digits
res = list(map(lambda x: x % (10 ** K), test_list))
 
# printing result
print("Rear K digits of elements ? : " + str(res))
#This code is contributed by Jyothi pinjala

Output
The original list is : [5645, 23567, 34543, 87652, 2342]
Rear K digits of elements ? : [645, 567, 543, 652, 342]

Time complexity: O(n), where n is the length of the test_list. The map function operates on each element in the test_list in O(1) time, and the list function takes O(n) time to create the final list.

Auxiliary Space: O(n), where n is the length of the test_list. The space required for the res list is O(n) and the space required for the test_list is O(n).

Method #4: Using a for loop and integer division and modulo operator

Step-by-step approach:

Below is the implementation of the above approach:




# initializing list
test_list = [5645, 23567, 34543, 87652, 2342]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using for loop and integer division and modulo operator to extract last K digits
res = []
for num in test_list:
    last_K_digits = num % (10 ** K)
    res.append(last_K_digits)
 
# printing result
print("Rear K digits of elements ? : " + str(res))

Output
The original list is : [5645, 23567, 34543, 87652, 2342]
Rear K digits of elements ? : [645, 567, 543, 652, 342]

Time complexity: O(n), where n is the length of the original list, since we’re looping through each element once.
Auxiliary space: O(n), since we’re creating a new list to store the result.

Method #5: Using list comprehension and string slicing




# initializing list
test_list = [5645, 23567, 34543, 87652, 2342]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using list comprehension and string slicing to extract last K digits
res = [int(str(num)[-K:]) for num in test_list]
 
# printing result
print("Rear K digits of elements ? : " + str(res))

Output
The original list is : [5645, 23567, 34543, 87652, 2342]
Rear K digits of elements ? : [645, 567, 543, 652, 342]

time complexity of this method is O(n*K) where n is the length of the list and K is the number of digits to be extracted. 
Auxiliary space complexity is O(n) because the result is stored in a list of size n.


Article Tags :