0

is there other way to do an matematic operational ( add, substract, multiply divided ) from an integer foreach value inside a list ?

for example:

a = 4
arr = list(20,1,5,36,10,31,100)

the a variable need to substract/multiply/divided to all values inside those list using for function like below snippet.

ar = []
for x in arr
    a = 4*x
    ar.append(a)

Is there any a better approach to solve this rather than using for/while loop ?

Gagantous
  • 432
  • 6
  • 29
  • 69

1 Answers1

1

Numpy or other vectorization methods notwithstanding, you're looking for list comprehensions.

arr = list(20,1,5,36,10,31,100)
quadrupled_arr = [x * 4 for x in arr]
AKX
  • 152,115
  • 15
  • 115
  • 172