I need to translate these two lines to mathematica from python

 gamma = np.array([u/np.linalg.norm(u) for u in x[:y]])
 beta = np.array([u/np.linalg.norm(u) for u in x[y:]])

where y is a number.

  • 5
    $\begingroup$ I guess ":" translates to ";;", a.k.a. Span. $\endgroup$ Mar 24, 2021 at 13:10
  • 6
    $\begingroup$ There are two off-by-one bugs possible here: Python a[x:y] means taking elements x to y-1, where the first element is element zero. Mathematica a[[x;;y]] means taking elements x to y, where the first element is element 1. So Python a[x:y] should be written as a[[x+1;;y]] in Mathematica. $\endgroup$
    – Roman
    Mar 24, 2021 at 13:14

1 Answer 1


: in python denotes ranges of array elements, and is equivalent to Span (;;) as noted by Henrik Schumacher's comment above

Note that Mathematica indexes from 1, whereas Python indexes from zero; additionally the array range specification in Python has the last term as an "up to but not including", whereas in Mathematica, it is inclusive of that element (as noted in Roman's comment above)

As best I understand your problem, it looks something like:

gamma = Norm/@x[[;;y-1]]
beta = Norm/@x[[y;;]]

But check your array indexing convention to make sure that the upper and lower bounds are what you intend them to be.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.