New course! Every coder should learn Generative AI!
Try a free lesson0
can I multiply?
can I multiply nums = [1, 2, 3] print(nums + [4, 5, 6]) result:[1,2,3,4,5,6] print(nums * [4,5,6]) result:?
3 Answers
+ 3
You can't multiply a list times a list like that. You can multiply a list times an integer. That list will be repeated n times into a new list.
+ 1
Why Don't you use code playground to see the output?
+ 1
class List(list):
def __mul__(self, other):
if (type(other) is int):
return super().__mul__(other)
return [x*y for x, y in zip(self, other)]
arr_1 = List([1, 2, 3, 4])
arr_2 = List([10, 20, 30, 40, 50])
print(arr_1*arr_2) # [10, 40, 90, 160]