You should probably take the list into the function as well. Then mutate it's elements and (optionally) return it ast well:
def num(data,multi):
"""Multiplies each element of data (a list of ints) by multi.
The original list is modified and returned as well for chaining."""
# data[:] = [d*multi for d in data] # inplace reassignment list comp
# or
for i,d in enumerate(data[:]): # this iterates a _copy_ of data, do not modify a list
data[i]=d*multi # that you currently iterate over - it is possible here
# but often it is a bad idea if you add/remove things
return data
S= [1,2,3,4]
num(S,2)
print(S)
Output:
[2,4,6,8]
There are several dupes out on SO:
Some of them for python2 where map
still returns a list
- you can look them up for even more options.
Returning the list inside the function as well, will allow you to chain calls:
num(num(num(S,2),3),4) # S = [24, 48, 72, 96]