Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Subtract K from each digit
In this article, we will learn how to write a Python program that subtracts a given value K from each digit of numbers in a list. When the result becomes negative, we use 0 instead.
Given an input list containing numbers and a K value, we subtract K from each individual digit and return the modified numbers.
Example
Let's understand with an example ?
Input list = [1034, 356, 2145, 6712, 8671] K value = 3
For the number 1034, we subtract 3 from each digit:
1 3 = 2 ? 0 (negative results become 0)
0 3 = 3 ? 0
3 3 = 0
4 3 = 1
Result: 0001 ? 1 (leading zeros removed)
Complete output: [1, 23, 12, 3400, 5340]
Method 1: Using For Loop
This approach converts each number to a string, processes each digit, and reconstructs the result ?
# input list
input_numbers = [1034, 356, 2145, 6712, 8671]
print("Input list:", input_numbers)
# value to subtract from each digit
k = 3
result_list = []
# process each number in the list
for number in input_numbers:
# convert number to string to access individual digits
str_number = str(number)
# subtract k from each digit, ensuring result is not negative
modified_digits = ''.join([str(max(0, int(digit) - k)) for digit in str_number])
# convert back to integer to remove leading zeros
result_list.append(int(modified_digits))
print("Result after subtracting K from each digit:", result_list)
Input list: [1034, 356, 2145, 6712, 8671] Result after subtracting K from each digit: [1, 23, 12, 3400, 5340]
Method 2: Using List Comprehension
A more concise approach using list comprehension for the same logic ?
# input list
input_numbers = [1034, 356, 2145, 6712, 8671]
print("Input list:", input_numbers)
k = 3
# process all numbers using list comprehension
result_list = [int(''.join([str(max(0, int(digit) - k)) for digit in str(num)]))
for num in input_numbers]
print("Result after subtracting K from each digit:", result_list)
Input list: [1034, 356, 2145, 6712, 8671] Result after subtracting K from each digit: [1, 23, 12, 3400, 5340]
Method 3: Using NumPy Array
Using NumPy arrays for digit-wise operations ?
import numpy as np
# input list
input_numbers = [1034, 356, 2145, 6712, 8671]
print("Input list:", input_numbers)
k = 3
result_list = []
for number in input_numbers:
# convert number to list of individual digits
digits = [int(digit) for digit in str(number)]
# create numpy array and subtract k
digit_array = np.array(digits) - k
# ensure no negative values
digit_array = np.maximum(digit_array, 0)
# reconstruct the number
result_number = int(''.join(map(str, digit_array)))
result_list.append(result_number)
print("Result after subtracting K from each digit:", result_list)
Input list: [1034, 356, 2145, 6712, 8671] Result after subtracting K from each digit: [1, 23, 12, 3400, 5340]
Comparison
| Method | Code Length | Best For |
|---|---|---|
| For Loop | Medium | Clear understanding of steps |
| List Comprehension | Short | Concise, Pythonic code |
| NumPy | Long | Large datasets, numerical operations |
Conclusion
All three methods effectively subtract K from each digit while handling negative results. List comprehension offers the most concise solution, while the for loop provides better readability for beginners.
---