Multiplication of a list and list of list

multiply two lists python
python list multiplication operator
python multiply two lists and sum
python multiplying lists together
multiply all elements of a list with a number python
write a python function to multiply all the numbers in a list
python list times number
multiplication in python

Mathematically I am trying to do a xij * yi multiplication. In Python, I need to multiply x and y lists as below:

x = [[1,2,3],[4,5,6],[7,8,9]]
y = [10,100,1000]

xy = [[] for i in range(3)]

for i in range(3):
   for j in range(3):
       xy[i][j] += [y[i] * x[i][j]]

However, I get "list index out of range" error while I expect to have the output as following:

xy = [[10, 20, 30],[400, 500, 600],[7000, 8000, 9000]]

Can you help?

You were very close. The issue was that you were using double indices [i][j] to refer to your nested lists. You have to just use the index i. The rest of your code is perfectly fine.

x = [[1,2,3],[4,5,6],[7,8,9]]
y = [10,100,1000]

xy = [[] for i in range(3)]

for i in range(3):
    for j in range(3):
        xy[i] += [y[i] * x[i][j]]

# [[10, 20, 30], [400, 500, 600], [7000, 8000, 9000]]

Alternative is to use append

for i in range(3):
    for j in range(3):
        xy[i].append(y[i] * x[i][j])

Alternative using NumPy

import numpy as n
x = np.array([[1,2,3],[4,5,6],[7,8,9]])
y = np.array([10,100,1000])
xy = (x.T*y).T

Python, Given a list, print the value obtained after multiplying all numbers in a list. Examples: Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 Input : list1  Traverse both lists and generate the required numbers to be multiplied and then return the multiplied values of the two numbers. Algorithm to generate the number from linked list representation: 1) Initialize a variable to zero 2) Start traversing the linked list 3) Add the value of first node to this variable 4) From the second node, multiply the variable by 10 first and then add the value of the node to this variable.

I think this will solve the problem,

x = [[1,2,3],[4,5,6],[7,8,9]] 
y = [10,100,1000]
xy = []
for i in range(3):
    xy.append([])
    for j in range(3):
        xy[i].append(y[i]*x[i][j])
print (xy)

In python, use 'list.append()' to add value to the list.

Python - Multiply two list, Python – Multiply two list. There can be many situations in which one requires to find index wise product of two different lists. This can have a possible  So, the list will be_ [1,2,3,4]_ when the loop ends. Print the list to the user. Declare one variable result as 1. This variable will hold the final multiplication result. We will multiply each element of the list and keep the result in this variable. Run one for loop. This loop will multiply all items of the list and save it in the result variable.

Let's use numpy library's multiply() function to solve this problem with the help of List comprehensions -

import numpy as np
list([np.multiply(x[i],y[i]).tolist() for i in range(len(y))])
    [[10, 20, 30], [400, 500, 600], [7000, 8000, 9000]]

How to multiply two lists in Python, Multiplication between two equal length lists multiplies each element from one list by the element at the same index in the other list. For example, multiplying [1,  Python – Constant Multiplication over List While working with the python lists, we can come over a situation in which we require to multiply constant to each element in the list. We possibly need to iterate and multiply constant to each element but that would increase the line of code.

Just simple list comprehensions:

x = [[1,2,3],[4,5,6],[7,8,9]]
y = [10,100,1000]

xy = [[y[i] * j for j in lst] for i, lst in enumerate(x)]
print(xy)

The output:

[[10, 20, 30], [400, 500, 600], [7000, 8000, 9000]]

Python List Operations: Concatenation, Multiplication, Slicing & del , Python List Operations: Concatenation, Multiplication, Slicing & del. Python; Lesson 12 of 24. Lists and strings have a lot in common. They are both sequences  Multiplication Tables 0-12. Online versions of all multiplication tables between 0 - 12. Sort. 1 Times Table 0 X 1 = 0 1 X 1 = 1 2 X 1 = 2 3 X 1 = 3

How to multiply two list in python., I tried to multiply two lists by * operator but in gives error. Also plz tell me the answers to the following question a = [1, 2, 3] b=[4, 5, 6] c= a*b  They are both sequences and, like pythons, they get longer as you feed them. Like a string, we can concatenate and multiply a Python list. Python List Concatenation & Multiplication. Old MacDonald had a farm, E-I-E-I-O. And on this farm there was a python, E-I-E-I-O. At the prompt, create a Python list with an item, ‘farm’:

How to get Python to multiply a list with another list, What do you mean multiply : If you mean the 1st element of the first list with the 1st element of the second , then use the zip function, that will return an iterable of​  Sometimes, when we are fed with the list of list, we need to multiply each of its element list with a particular element fed by the order in the other list. This particular problem is very specific but knowledge of it can be useful in such cases. Let’s discuss certain ways in which this can be done. Method #1 : Using loops

Python Language, Consider the case of creating a nested list structure by multiplying: li = [[]] * 3 print​(li) # Out: [[], [], []]. At first glance we would think we have a list of containing 3  Firstly, set the list − List<int> myList = new List<int> { 5, 10, 7 }; Now, set the value of a variable to 1 that would help us in multiplying − int prod = 1; Loop through and get the product − foreach(int i in myList) { prod = prod*i; } The following is the code − Example. Live Demo

Comments
  • Brilliant catch! Thanks!