0

I wrote a program that takes two lists and multiplies each index in the first list by the every index in the second list. Once the second list reaches the last index the index in the first list is incremented to the next index and repeats the process.

What I am trying to do now is have it be recursive so that the number of lists won't matter and the program can enumerate itself recursively. I also want to show what what multiplication is being outputted. For now the output shows all the answers in a list but I also want to show it in the output say for example:

1 * 4 = 4 As the 0 index in the first list is 1 and the 0 index in the second list is 4. 5 * 11 = 55 1 index of first list times 2 index of the second list

Below is the full working code as well as the output:

Code:

list1 = [1, 5, 16, 14, 21]
list2 = [4, 7, 11, 14, 17]

# store multiplied values in new list
results = []

# assign two temp variables to keep track of each list index
temp1 = 0
temp2 = 0

# loop through each index of the first list
while temp1 <= len(list1) - 1:
    # print(var1[temp1]*var2[temp2])
    # add to results list the current index of list 1 * the current index of temp 2
    results.append(list1[temp1]*list2[temp2])
    # increase the index of the temp variables to traverse both lists in order
    # temp2 gets incremented until it reaches the end last index of list2
    # once it does increase temp1 by 1 to move onto the next index in list1 and repeat the process
    if temp2 == len(list2) - 1:
        temp1 += 1
        temp2 = 0
    else:
        temp2 += 1

print(results)

Output: [4, 7, 11, 14, 17, 20, 35, 55, 70, 85, 64, 112, 176, 224, 272, 56, 98, 154, 196, 238, 84, 147, 231, 294, 357]

  • It's not clear to me how you intend for this to be generalized. What should the calculation look like when a third list is added? – Karl Knechtel Feb 03 '22 at 00:49
  • If a third list were to be added for example it would be (based on the lists I have above) 1 * 4 * (whatever number is the first index in the 3rd list) –  Feb 03 '22 at 00:51
  • And if there were also five values in the third list, you would expect to sum up 125 product values? Then the operation you are looking for is called the *cartesian product*, and there are many duplicate questions for this already. You may also consider using a search engine on the rest of the Internet. – Karl Knechtel Feb 03 '22 at 00:52

1 Answers1

1

While you asked for a recursive implementation, I'm going to direct you to an alternative itertools.product which will be both faster and just as generalizable.

In an example:

from itertools import product

list1 = [1, 5, 16, 14, 21]
list2 = [4, 7, 11, 14, 17]
result = [x * y for x, y in product(list1, list2)]

This can then be generalized to more arguments, i.e. product(list1, list2, list3, list4, ...) or using unpacking: product(*list_of_lists).

from math import prod

result = [prod(nums) for nums in product(...)]

Output:

>>> print(result)
[4, 7, 11, 14, 17, 20, 35, 55, 70, 85, 64, 112, 176, 224, 272, 56, 98, 154, 196, 238, 84, 147, 231, 294, 357]
Dillon Davis
  • 6,679
  • 2
  • 15
  • 37