Python – Index Value repetition in List
Given a list of elements, The task is to write a Python program to repeat each index value as per the value in that index.
Input : test_list = [3, 0, 4, 2]
Output : [0, 0, 0, 2, 2, 2, 2, 3, 3]
Explanation : 0 is repeated 3 times as its index value is 3.Input : test_list = [3, 4, 2]
Output : [0, 0, 0, 1, 1, 1, 1, 2, 2]
Explanation : 1 is repeated 4 times as its value is 4.
Method #1: Using list comprehension + enumerate()
In this, we perform task of repetition using * operator, and enumerate() is used to get indices along with values for repetition. List comprehension is used to iteration all the elements.
Python3
# Python3 code to demonstrate working of # Index Value repetition in List # Using list comprehension + enumerate() # initializing Matrix test_list = [ 3 , 0 , 4 , 2 ] # printing original list print ( "The original list is : " + str (test_list)) # enumerate() gets index and value of similar index element res = [ele for sub in ([idx] * ele for idx, ele in enumerate (test_list)) for ele in sub] # printing result print ( "Constructed List : " + str (res)) |
The original list is : [3, 0, 4, 2] Constructed List : [0, 0, 0, 2, 2, 2, 2, 3, 3]
Method #2: Using chain.from_iterable() + list comprehension
In this, we perform the last step of flattening of list using chain.from_iterable(). List comprehension performs the task of iteration of all the elements.
Python3
# Python3 code to demonstrate working of # Index Value repetition in List # Using chain.from_iterable() + list comprehension import itertools # initializing Matrix test_list = [ 3 , 0 , 4 , 2 ] # printing original list print ( "The original list is : " + str (test_list)) # enumerate() gets index and # value of similar index element # from_iterable() used to flatten res = list (itertools.chain( * ([idx] * ele for idx, ele in enumerate (test_list)))) # printing result print ( "Constructed List : " + str (res)) |
The original list is : [3, 0, 4, 2] Constructed List : [0, 0, 0, 2, 2, 2, 2, 3, 3]
Method #3: Using extend() method
In this, we perform task of repetition using * operator, and add the elements using the extend() method in the new list.
Python3
# Python3 code to demonstrate working of # Index Value repetition in List # initializing Matrix test_list = [ 3 , 0 , 4 , 2 ] # printing original list print ( "The original list is : " + str (test_list)) res = [] for i in range ( 0 , len (test_list)): res.extend([i] * test_list[i]) # printing result print ( "Constructed List : " + str (res)) |
The original list is : [3, 0, 4, 2] Constructed List : [0, 0, 0, 2, 2, 2, 2, 3, 3]
Please Login to comment...