Python Program to Subtract K from each digit


In this article, we will learn a python program to subtract K from each digit in a list.

Assume we have taken an input list containing numbers and K value. We will now subtract k from each digit of a list element and print the resultant list using the below methods.

Example

Let inputList = [1034, 356, 2145, 6712, 8671]
Given input k value = 3

Now we consider 1st element i.e, 1034

Subtract 3 from each digit of 1034 i.e,

  • 1-3 =-2. So, it is ceiled to 0

  • 0-3 = -3. So, it is ceiled to 0

  • 3-3 = 0. It has no value

  • 4-3 = 1. Hence the value is retained

Hence the output for this 1034 is 0001 i.e 1. Similarly get all values for the other list elements.

So, the output list is: [1, 23, 12, 3400, 5340]

Methods Used

  • Using for loop, str(), – operator

  • Using list comprehension, str(), – operator

  • Using Numpy ‘-’ Operator.

Method 1: Using for loop, str(), – operator

The max() method(returns the highest-valued item/greatest numbers in an iterable). str() function(returns the string format of the object i.e converts it into a string).

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input list and print it.

  • Create another variable to store the input k value to be subtracted from each digit of a list element.

  • Create a new empty list for storing a resultant list of elements after subtraction of k from each digit.

  • Use the for loop to traverse through the elements of the input list.

  • Convert the number to the string using the str() function.

  • Traverse over the string's digits. Subtract k from each digit to find the maximum of (0, result).

  • Join the result after subtraction using the join() function.

  • Convert the result to an integer to remove leading zeros using the int() function.

  • Add the result to the resultList using the append() function.

  • Print the resultant list after subtracting K from each digit.

Example

The following program returns a list after subtracting given k from digits of input list elements using for loop, str(), – operator –

# input list
inputList = [1034, 356, 2145, 6712, 8671]

# printing the input list
print("Input list:\n", inputList)

# input k value (to be subtracted)
k = 3

# resultant list for storing elements after subtraction of k from each digit
resultList = []

# traversing through the elements of input list
for item in inputList:
   
   # Converting the number to a string
   strItem = str(item)

   # Traversing through the digits of the string
   
   # Subtracting k from each digit and finding max of 0 and this result.
   
   # Joining all the results using the join() function
   reslt = ''.join([str(max(0, int(i) - k)) for i in strItem])
   
   # Converting the result to an integer and adding it to the result list
   resultList.append(int(reslt))
   
# printing resultant list after subtracting K from each digit
print("Resultant list after subtracting K from each digit:\n", resultList)

Output

On executing, the above program will generate the following output –

Input list:
[1034, 356, 2145, 6712, 8671]
Resultant list after subtracting K from each digit:
[1, 23, 12, 3400, 5340]

Method 2: Using list comprehension, str(), – operator

list comprehension

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

Example

The following program returns a list after subtracting given k from digits of input list elements using list comprehension, str(), – operator –

# input list
inputList = [1034, 356, 2145, 6712, 8671]

# printing the input list
print("Input list:\n", inputList)

# input k value (to be subtracted)
k = 3

# Finding result using the list comprehension(concise syntax)
resultList = [int(''.join([str(max(0, int(i) - k)) for i in str(i)])) for i in inputList]

# printing resultant list after subtracting K from each digit
print("Resultant list after subtracting K from each digit:\n", resultList)

Output

On executing, the above program will generate the following output –

Input list:
[1034, 356, 2145, 6712, 8671]
Resultant list after subtracting K from each digit:
[1, 23, 12, 3400, 5340]

Method 3: Using Numpy ‘-’ Operator

The following program returns a list after subtracting given k from digits of input list elements using Numpy ‘-’ Operator –

import numpy

# input list
inputList = [1034, 356, 2145, 6712, 8671]

# printing the input list
print("Input list:\n", inputList)

# input k value (to be subtracted)
k = 3

# resultant list for storing elements after subtraction of k from each digit
resultList = []

# traversing through the elements of the input list
for item in inputList:
   
   # Creating a new list to store the list of digits
   listOfDigits = []
   
   # Traversing through the digits of the list
   for digit in str(item):
      
      # Converting the digit to an integer and appending it to the list
      listOfDigits.append(int(digit))
   
   # Converting the above list of digits to Numpy Array
   numpyArray = numpy.array(listOfDigits)

   # Subracting K from each digit
   numpyArray = numpyArray - k
   
   # Taking an empty string to store the result
   reslt = ""

   # Traversing through the digits of the Numpy Array
   for digit in numpyArray:
      reslt += str(max(0, digit))
   # Converting the result to an integer and adding it to the result list
   resultList.append(int(reslt))

# printing resultant list after subtracting K from each digit
print("Resultant list after subtracting K from each digit:\n", resultList)

Output

On executing, the above program will generate the following output –

Input list:
[1034, 356, 2145, 6712, 8671]
Resultant list after subtracting K from each digit:
[1, 23, 12, 3400, 5340]

Conclusion

In this article, we learned how to subtract k using 3 different methods from each digit in the given list of numbers. List comprehension provides a concise syntax that enables us to complete the work with fewer lines of code, as we have learned. We also learned how to obtain the string of digits, convert them to a Numpy array, and use the '-' operator to subtract k from each element of the Numpy array.

Updated on: 31-Jul-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements