How do you subtract a value from all elements in a list python?

I have a list

 a = [49, 51, 53, 56]

How do I subtract 13 from each integer value in the list?

asked Feb 7, 2011 at 5:50

If are you working with numbers a lot, you might want to take a look at NumPy. It lets you perform all kinds of operation directly on numerical arrays. For example:

>>> import numpy
>>> array = numpy.array([49, 51, 53, 56])
>>> array - 13
array([36, 38, 40, 43])

answered Feb 7, 2011 at 6:22

shangshang

24.4k3 gold badges57 silver badges86 bronze badges

1

You can use map() function:

a = list(map(lambda x: x - 13, a))

answered Feb 7, 2011 at 8:31

sputnikussputnikus

5831 gold badge3 silver badges12 bronze badges

1

To clarify an already posted solution due to questions in the comments

import numpy

array = numpy.array([49, 51, 53, 56])
array = array - 13

will output:

array([36, 38, 40, 43])

How do you subtract a value from all elements in a list python?

csabinho

1,5671 gold badge17 silver badges26 bronze badges

answered Nov 8, 2019 at 0:24

JJ K.JJ K.

611 silver badge3 bronze badges

This will work:

for i in range(len(a)):
  a[i] -= 13

answered Feb 7, 2011 at 5:58

How do you subtract a value from all elements in a list python?

Oscar MederosOscar Mederos

28.3k21 gold badges81 silver badges123 bronze badges

4

In this article, we will learn to perform subtraction on a list in Python. We will use some built-in functions and some custom code as well. Let's first have a quick look over what is list and list subtraction in Python.

Python List

Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values.

list1 = ["Ram","Arun","Kiran"]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]

List Subtraction

List subtraction uses two lists and returns a new list after subtraction. This is similar to performing a subtraction operation in mathematics. It finds the difference between each element at each index from the given two lists.

#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]

#output
list3 = [3,4,-1,3,-2]

New List = Elements of List1 - Elements of List2

To perform list subtraction, the two input lists must be of the same length and it should contain elements of the same type i.e. both lists must contain only numerical values. The given example subtracts the elements at each index in one list from the other list.

Example: List Subtraction Using zip() and "-" operator

This method uses zip(iterator1, iterator2) with the list1 as iterator1 and list2 as iterator2 to return a zip object. It uses a for-loop to iterate over the zip object and subtract the lists' elements from each other and store the result in a new list.

#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]

#empty list
sub_list = []

#two lists are passed to zip 
zip_object = zip(list1, list2)

#loop to find diff of each element
for list1_i, list2_i in zip_object:
    sub_list.append(list1_i - list2_i)

print(sub_list)


[3, 4, -1, 3, -2]

Conclusion

In this article, we learned to find the difference between each element of two lists by traversing them using a zip object. We learned to perform list subtraction over each element.

How do you subtract elements from a list in Python?

Use zip() to subtract two lists.
list1 = [2, 2, 2].
list2 = [1, 1, 1].
difference = [] initialization of result list..
zip_object = zip(list1, list2).
for list1_i, list2_i in zip_object:.
difference. append(list1_i-list2_i) append each difference to list..
print(difference).

How do you subtract a value in Python?

To subtract two numbers in Python, use the subtraction(-) operator. The subtraction operator (-) takes two operands, the first operand on the left and the second operand on the right, and returns the difference of the second operand from the first operand.

How do you subtract a number from an array in Python?

subtract() in Python. numpy. subtract() function is used when we want to compute the difference of two array.It returns the difference of arr1 and arr2, element-wise.

How do you subtract all the elements in an array?

Find the minimum non-zero element in the array, print it and then subtract this number from all the non-zero elements of the array. If all the elements of the array are < 0, just print 0.