112

I have a list

 a = [49, 51, 53, 56]

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

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
jaycodez
  • 1,867
  • 6
  • 21
  • 28

5 Answers5

192

With a list comprehension:

a = [x - 13 for x in a]
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thanks, Good work. :) How do you put that into a function quickly? – jaycodez Feb 07 '11 at 05:59
  • 2
    By putting `def minus13(a):` on the line above and indenting one level. – Ignacio Vazquez-Abrams Feb 07 '11 at 06:00
  • 8
    Why `a[:]` on the lhs instead of just assigning back to `a`? Doesn't `a[:]` create a copy of the list? – istruble Feb 07 '11 at 06:06
  • 2
    istruble: http://stackoverflow.com/questions/4081561/what-is-the-difference-between-list-and-list-in-python – Ignacio Vazquez-Abrams Feb 07 '11 at 06:08
  • 4
    What *should* be done is that the value from the list comprehension should be returned by the function, and the caller should decide to replace the existing sequence if appropriate. – Ignacio Vazquez-Abrams Feb 07 '11 at 06:15
  • IVA: Thanks. That is what I was guessing but I was not finding much on docs.python.org with my initial searches. It just seemed like possible early optimizing ;) Josh: I was writing the same answer but with just `a` on the lhs. – istruble Feb 07 '11 at 06:37
  • `minus13()` is a function you define in your code, which returns the result of the list comprehension. The rest is the creation of the list and the invocation of the function which occurs elsewhere in your code. – Ignacio Vazquez-Abrams Feb 07 '11 at 07:47
  • 1
    @istruble: Just assigning directly to a will work just as well. So `a = [x - 13 for x in a]` will work just as well, and is less confusing. – Lennart Regebro Feb 07 '11 at 08:35
  • 1
    @Lennart: Unless other references to the list exist, and you want them to contain the modified values. – Ignacio Vazquez-Abrams Feb 07 '11 at 08:37
  • 1
    @Ignacio: True, but that was not in the question, and and in my experience it's not a very common usecase. Maybe it depends on your style, but I never to that. – Lennart Regebro Feb 07 '11 at 08:46
81

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])
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
shang
  • 24,642
  • 3
  • 58
  • 86
12

You can use map() function:

a = list(map(lambda x: x - 13, a))
sputnikus
  • 583
  • 1
  • 3
  • 12
8

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])

csabinho
  • 1,579
  • 1
  • 18
  • 28
JJ K.
  • 81
  • 1
  • 3
2

This will work:

for i in range(len(a)):
  a[i] -= 13
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
  • 4
    The list comprehension solution is much more pythonic. You might like them. http://docs.python.org/tutorial/datastructures.html#list-comprehensions – istruble Feb 07 '11 at 06:41
  • Who deleted my comment? - I don't see the point in counting the length of a. To make a simple calculation. – jaycodez Feb 07 '11 at 07:16
  • 2
    The count is necessary, if the algorithm is to be expressed this way, for an index variable (`i`) to iterate through all possible index values. This index variable is needed to mutate each element in the list. – Santa Feb 07 '11 at 08:05
  • Using range and len together in python is almost always a code smell, invariably there's a better way of doing things as in the numerous examples given above. – Mark Lawrence Feb 20 '21 at 15:53