Exercise 11

Write an algorithm in python as a function which takes as parameter a tuple (L, n) formed by a list L of numbers and an integer n and which returns the list obtained from L by multiplying its elements by n . Example if L = [3, 9, 5, 23] and n = 2 the function returns the list: [6, 18, 10, 46]

Solution

def listMultiply (L, n):
# initialization of the searched list
l_mult = []
# iterate over and multiply the elements of the list by n
for x in L:
l_mult.append (n * x)

return l_mult

# Example
n = 2
L = [3, 9, 5, 23]
print (listMultiply (L, n)) # the out put is : [6, 18, 10, 46]

Younes Derfoufi
my-courses.net
One thought on “Solution Exercise 11: Multiply the elements of a Python list by a given integer”

Leave a Reply