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
test_list = [ 5645 , 23567 , 34543 , 87652 , 2342 ]
print ( "The original list is : " + str (test_list))
K = 3
res = [ele % ( 10 * * K) for ele in test_list]
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
test_list = [ 5645 , 23567 , 34543 , 87652 , 2342 ]
print ( "The original list is : " + str (test_list))
K = 3
res = [ int ( str (idx)[ - K:]) for idx in test_list]
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.
Python3
test_list = [ 5645 , 23567 , 34543 , 87652 , 2342 ]
print ( "The original list is : " + str (test_list))
K = 3
res = list ( map ( lambda x: x % ( 10 * * K), test_list))
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 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
test_list = [ 5645 , 23567 , 34543 , 87652 , 2342 ]
print ( "The original list is : " + str (test_list))
K = 3
res = []
for num in test_list:
last_K_digits = num % ( 10 * * K)
res.append(last_K_digits)
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
- 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
test_list = [ 5645 , 23567 , 34543 , 87652 , 2342 ]
print ( "The original list is : " + str (test_list))
K = 3
res = [ int ( str (num)[ - K:]) for num in test_list]
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.
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 Numbers from String
Many times, while working with strings we come across this issue in which we need to get all the numeric occurrences. This type of problem generally occurs in competitive programming and also in web development. Let's discuss certain ways in which this problem can be solved in Python. Example Use a
5 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 u
2 min read
Python | Extract Numbers in Brackets in String
Sometimes, while working with Python strings, we can have a problem in which we have to perform the task of extracting numbers in strings that are enclosed in brackets. Let's discuss the certain ways in which this task can be performed. Method 1: Using regex The way to solve this task is to construc
6 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
Python - Extract element from list succeeded by K
Given a list, extract the elements which are having K as the next element. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5], K = 3 Output : [2, 5] Explanation : Elements before 3 are 2, 5. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 8], K = 8 Output : [7, 3] Explanation : Elements before 8 are 7, 3. Metho
5 min read
Python - Split Numeric String into K digit integers
Given a String, convert it to K digit integers Input : test_str = '457336', K = 2 Output : [45, 73, 36] Explanation : Divided in 2 digit integers. Input : test_str = '457336', K = 3 Output : [457, 336] Explanation : Divided in 3 digit integers. Method #1 : Using int() + slice + loop In this, we iter
5 min read
Add Comma Between Numbers - Python
When working with large numbers in Python, adding commas for better readability can make them easier to understand. In this article, weâll explore simple ways to add commas between numbers, helping make large numbers more readable and neatly formatted. Using format() format() function in Python prov
3 min read