Change language

# Python | Custom Multiplication in a List of Lists

| |

Method # 1: Using Loops

This Naive and brute force method does this particular task in which we run a loop to get all the elements and its nested components and respectively multiply.

Output:

` The original list: [[5, 6, 8], [7, 4, 3], [8, 10 , 12]] The original multiply list: [10, 20, 30] The list after multiply: [[50, 60, 80], [140, 80, 60], [240, 300, 360]] `

Method # 2: Using the comprehension list + `enumerate()`

This problem can also be solved in a shorter way using the function ` enumerate ` to get the indexes and value of the container in one go. This is a one-line approach to solve this problem.

 ` # Python3 demo code ` ` # Custom Multiplication in the list of lists ` ` # Using loops `   ` # initializing list ` ` test_list ` ` = ` ` [[` ` 5 ` `, ` ` 6 ` `, ` ` 8 ` `], [` ` 7 ` `, ` ` 4 ` `, ` ` 3 ` `], [` ` 8 ` `, ` ` 10 ` `, ` ` 12 ` `]] `   ` # initialize the multiplication list ` ` mult_list ` ` = ` ` [` ` 10 ` `, ` ` 20 ` `, ` ` 30 ` `] `   ` # print original list ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list)) `   ` # print the multiplication list ` ` print ` ` (` ` "The original multiply list:" ` ` + ` ` str ` ` (mult_list)) ` ` `  ` # using loops ` ` # Custom Multiplication in a list of lists ` ` res ` ` = ` ` [[] ` ` for ` ` idx ` ` in ` ` range ` ` (` ` len ` ` (test_list))] ` ` for ` ` i ` ` in ` ` range ( len (test_list)): `` for j in range ( len (mult_list)): res [i] + = [mult_list [i] * test_list [i] [j]]   # print result print ( " The list after multiply: " + str (res) ) `
 ` # Python3 demo code ` ` # Custom Multiplication in the list of lists ` ` # Using the comprehension list + enumerate () ` ` `  ` # initializing list ` ` test_list ` ` = ` ` [[` ` 5 ` `, ` ` 6 ` `, ` ` 8 ` `], [` ` 7 ` `, ` ` 4 ` `, ` ` 3 ` `], [` ` 8 ` `, 10 , 12 ]] ``   # initialize the multiplication list mult_list = [ 10 , 20 , 30 ]   # print original list print ( " The original list: " + str (test_list))   # print the multiplication list print ( "The original multiply list:" + str (mult_list))   # using a list comprehension + enumerate () # Custom Multiplication in a list of lists res = [[mult_list [i] * j for j in sub]    for i, sub in enumerate (test_list)]    # print result print ( "The list after multiply:" + str (res)) `

Output:

` The original list: [[5, 6, 8], [7, 4, 3], [8, 10, 12]] The original multiply list: [10, 20, 30] The list after multiply: [[50, 60, 80] , [140, 80, 60], [240, 300, 360]] `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries