5.9k views
1 vote
Given two lists of integers, write a function multLists(list1, list2) that multiplies each value of the list1 by the values of list2 and returns list1 with it's updated values. The lists will be obtained for you, just write the function and print the result.

User Aaeb
by
6.2k points

1 Answer

4 votes

Answer:

FIRST SOLUTION

def multLists(list1,list2):

for i in range(len(list1)):

list1.append(list1[i]*list2[i])

return list1

SECOND SOLUTION

def multLists(list1,list2):

newlist = [ ]

for i in range(len(list1)):

newlist.append(list1[i]*list2[i])

return newlist

Step-by-step explanation:

First we define the function and call it MultLists. Then using a for statement, we loop through the list the number of times that is equivalent to the length of the list1. In the first solution, we append the element-wise multiplication in the two list and append this to list1. The return of the first solution, is list1 which contains list1 in addition to the product of the elements from list1 and list2

In the second solution, we create a new list, and using the append function, we populate it with the values of the element-wise multiplication from the two lists. The return of the second solution, is a new containing only to the product of the elements from list1 and list2

User Ahmet Recep Navruz
by
6.2k points