New course! Every coder should learn Generative AI!
Try a free lesson+ 6
Nana Kwabena Andoh ,
may be i have misunderstood something, but for me the task is to multiply 2 arrays.
So the basic case is to use two 1D arrays with the same length. By using zip() we don't even have to care about on the same length of the arrays, as zip() can handle this. Using sample 2, where list1 has 4 elements, and list2 has 6 elements, only the first 4 elements will be used.
If all elememts from the longest list should be used, itertools.ziplongest() can handle this. You can find more about this in the python docs.
(if special cases like uneven shapes, uneven number of elements or multi-dimensional arrays has to be handled, i would recommend numpy, which is the de facto standard for this kind of tasks.)
list1 = [2,0,3,1,5,8]; list2 = [7,6,2,4,7,9] # sample 1 result: [14, 0, 6, 4, 35, 72]
list1 = [2,0,3,1]; list2 = [7,6,2,4,7,9] # sample 2 result: [14, 0, 6, 4]
res = [i * j for i,j in zip(list1, list2)]
print(res)
# in one line only for print:
print([i * j for i,j in zip(list1, list
+ 3
Please show your attempt first. Otherwise we cannot help you.