Reputation: 65
I am trying to multiply and append 2 lists. I can't understand how it works.
xs = [1,2,3,4]
m = 3
t.append(xs[m])
return t
How does ([1,2,3,4] * [3]) = 4
?
Upvotes: 0
Views: 1364
Reputation: 798576
Performing i[j]
indexes the sequence i
with the value j
. If you want matrix multiplication then you should look at NumPy.
>>> [1, 2, 3, 4][3]
4
Upvotes: 3
Reputation: 55
An addition to the list multiplication, we cannot multiply two lists. We can multiply list by a number(Integer; -ve will do as well). Doing this you can repeat or replicate your list may times as:
[1, 2, 3, 4] * 3 OR
3 * [1, 2, 3, 4]
will produce
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Upvotes: 0