can I multiply? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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:?

23rd Apr 2020, 9:06 AM
stage royal
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.
23rd Apr 2020, 9:12 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Why Don't you use code playground to see the output?
23rd Apr 2020, 9:36 AM
Abhay
Abhay - avatar
+ 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]
23rd Apr 2020, 4:04 PM
Zuke
Zuke - avatar