Python - Extract Rear K digits from Numbers
Last Updated :
09 Apr, 2023
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
# 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))
OutputThe 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
# 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))
OutputThe 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.
Python3
# 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
OutputThe 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:
- Initialize an empty list to store the result.
- Loop through each element in the original list.
- Use integer division (//) and modulo (%) operator to extract the last K digits of each element.
- Append the extracted digits to the result list.
- Return the result list.
Below is the implementation of the above approach:
Python3
# 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))
OutputThe 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
- Initialize the input list test_list with the given integers.
- Print the original list to confirm the input.
- Initialize the value of K to the given integer value.
- Use list comprehension to iterate over each number in test_list.
- Convert each number to a string using the str() function.
- Use string slicing to extract the last K characters from each string using the [-K:] syntax.
- Convert the resulting substring back to an integer using the int() function.
- Store the resulting integers in a list using the list comprehension syntax.
- Print the resulting list of integers to confirm the output.
Python3
# 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))
OutputThe 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.
Similar Reads
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 - 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 - Extract numbers from list of strings We are given a list of string we need to extract numbers from the list of string. For example we are given a list of strings s = ['apple 123', 'banana 456', 'cherry 789'] we need to extract numbers from the string in list so that output becomes [123, 456, 789].Using Regular ExpressionsThis method us
2 min read
Python Program to Subtract K from each digit Given a list, the task is to write a Python Program to subtract K from each digit, if the element gets below 0, retain 0. Examples: Input : test_list = [2345, 8786, 2478, 8664, 3568, 28], K = 4Output : [1, 4342, 34, 4220, 124, 4]Explanation : In 2345, 4 subtracted from 2 is -2, hence ceiled to 0. He
5 min read
Python | Ways to remove numeric digits from given string In Python, we can remove numeric digits from a string in multiple ways. For example, if we have a string like "Geeks123" and we want to remove the numbers to get "Geeks", we can use different methods to accomplish this. We can do this by using techniques such as list comprehension, regular expressio
3 min read
Sum of number digits in List in Python Our goal is to calculate the sum of digits for each number in a list in Python. This can be done by iterating through each number, converting it to a string, and summing its digits individually. We can achieve this using Pythonâs built-in functions like sum(), map(), and list comprehensions. For exa
2 min read