Method: Using a list comprehension
This task can be done in a crude way, but it is always better to have a shorter implementation using a list comprehension. In this we perform the task in 2 steps, first we create a list of helpers to form a list of multiplication factors, and then we accumulate the result using the original list.
# Python3 demo code # Repeat and multiply list extension # Using comprehension list # initializing list test_list = [ 4 , 5 , 6 ] # print original list code> print ( "The original list is:" + str (test_list)) # Expansion factor N = 4 # Multiplication factor M = 3 # Repeat and multiply the list extension # Using the comprehension list temp = [ 1 * M * * i for i in range (N)] res = list ([ele * tele for tele in temp for ele in test_list]) # print result print ( "List after extension and multiplication:" + str (res)) |
Output:
The original list is: [4, 5, 6] List after extension and multiplication: [4, 5, 6, 12, 15, 18, 36, 45, 54, 108, 135, 162]
Python | Repeat and multiply list expansion exp: Questions
Python | Repeat and multiply list expansion Python functions: Questions