Change language

Python | Repeat and multiply list expansion

| |

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

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

Shop

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

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method