Python | Custom Multiplication in list of lists
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
This is the Naive and brute force method perform this particular task in which we run a loop to get all the elements and it’s nested components and multiply accordingly.
# Python3 code to demonstrate # Custom Multiplication in list of lists # Using loops # initializing list test_list = [[5, 6, 8], [7, 4, 3], [8, 10, 12]] # initializing multiply list mult_list = [10, 20, 30] # printing original list print("The original list : " + str(test_list)) # printing multiply list print("The original multiply list : " + str(mult_list)) # using loops # Custom Multiplication in 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)) |
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 list comprehension + enumerate()
This problem can also be solved in a shorter way using the power of enumerate function to get the indices and value of the container at one time. This is one-liner approach to solve this problem.
# Python3 code to demonstrate # Custom Multiplication in list of lists # Using list comprehension + enumerate() # initializing list test_list = [[5, 6, 8], [7, 4, 3], [8, 10, 12]] # initializing multiply list mult_list = [10, 20, 30] # printing original list print("The original list : " + str(test_list)) # printing multiply list print("The original multiply list : " + str(mult_list)) # using list comprehension + enumerate() # Custom Multiplication in 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)) |
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]]
Recommended Posts:
- Python | Custom list split
- Python | Program to count number of lists in a list of lists
- Python | Sorting list of lists with similar list elements
- Python | Merge List with common elements in a List of Lists
- Python | Check if a list exists in given list of lists
- Python | Ways to sum list of lists and return sum list
- Python | Maximum sum of elements of list in a list of lists
- Python | Convert list into list of lists
- Python | Split list into lists by particular value
- Python | Merge two lists into list of tuples
- Python | Convert a list into tuple of lists
- Python | Column deletion from list of lists
- Python | Remove given element from list of lists
- Python | Merge two list of lists according to first element
- Python | Get positive elements from given list of lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



