2

I'm coming from a MATLAB background and this simple operation so far seems to be extremely complicated to implement in Python, according to the answers in other stacks. Typically, most answers use a for loop.

The best I've seen so far is

import numpy
start_list = [5, 3, 1, 2, 4]
b = list(numpy.array(start_list)**2)

Is there a simpler way?

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
user1936752
  • 756
  • 1
  • 9
  • 25
  • 2
    Why not just use a numpy array altogether? – Padraic Cunningham Feb 29 '16 at 10:32
  • 1
    @PadraicCunningham: I do not think this question is about numpy (even though it was tagged as such). I think that the numpy way was the most compact one found by the OP – user16538 Feb 29 '16 at 10:34
  • I added the `numpy` tag, the original question imports `numpy` and uses `numpy.array`, also the OP says to be coming from a MATLAB background, seems fitting IMO. – bakkal Feb 29 '16 at 10:35
  • 2
    @user16538. the OP is already using numpy and seem to be doing numeric calculations so it is definitely related to numpy and it would make a lot more sense to just use numpy arrays unless there is some specific reason why they cannot. – Padraic Cunningham Feb 29 '16 at 10:37
  • The suggested duplicate I linked is valid for _vanilla_ Python answers (list comprehension, map + lambda) etc only. It is not valid for `numpy`. – bakkal Feb 29 '16 at 11:04
  • 2
    NumPy is definitely the tool that you need to do Matlab-like work in Python. I found https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html very helpful when I made the switch. – mtrw Feb 29 '16 at 11:28
  • I'm learning Python guys, and it's only been a week. But still it surprises me that while MATLAB can simply do x.^2, there isn't a simple thing like that in Python. – user1936752 Mar 01 '16 at 11:24
  • Use JUST this: `myarray ** 2` – seralouk Nov 27 '19 at 09:40

4 Answers4

7

Note: Since we already have duplicates for the vanilla Python, list comprehensions and map and that I haven't found a duplicate to square a 1D numpy array, I thought I'd keep my original answer that uses numpy

numpy.square()

If you're coming from MATLAB to Python, you are definitely correct to try to use numpy for this. With numpy you can use numpy.square() which returns the element-wise square of the input:

>>> import numpy as np
>>> start_list = [5, 3, 1, 2, 4]
>>> np.square(start_list)
array([25,  9,  1,  4, 16])

numpy.power()

There is also a more generic numpy.power()

>>> np.power(start_list, 2)
array([25,  9,  1,  4, 16])
Community
  • 1
  • 1
bakkal
  • 54,350
  • 12
  • 131
  • 107
5

The most readable is probably a list comprehension:

start_list = [5, 3, 1, 2, 4]
b = [x**2 for x in start_list]

If you're the functional type, you'll like map:

b = map(lambda x: x**2, start_list)  # wrap with list() in Python3
L3viathan
  • 26,748
  • 2
  • 58
  • 81
5

Just use a list comprehension:

start_list = [5, 3, 1, 2, 4]
squares = [x*x for x in start_list]

Note: as a minor optimization, doing x*x is faster than x**2 (or pow(x, 2)).

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
4

You could use list comprehension:

[i**2 for i in start_list]

Or if you're using numpy you could use tolist method:

In [180]: (np.array(start_list)**2).tolist()
Out[180]: [25, 9, 1, 4, 16]

Or np.power:

In [181]: np.power(start_list, 2)
Out[181]: array([25,  9,  1,  4, 16], dtype=int32)
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93