Given a List, the following program repeats those elements which are at a custom index, these custom indices are provided to it as a separate list.
Input : test_list = [4, 6, 7, 3, 1, 9, 2, 19], idx_list = [3, 1, 6]
Output : [4, 6, 6, 7, 3, 3, 1, 9, 2, 2, 19]
Explanation : All required index elements repeated, Eg. 6 repeated as it is at index 1.
Input : test_list = [4, 6, 7, 3, 1, 9, 2, 19], idx_list = [1, 6]
Output : [4, 6, 6, 7, 3, 1, 9, 2, 2, 19]
Explanation : All required index elements repeated, 6 repeated as it is at index 1.
In this, we perform the task of repeating each element in case it is the required index to be repeated using extend() and loop is used to iterate for every index. The enumerate() is used to get all the indices along with elements.
The original list is : [4, 6, 7, 3, 1, 9, 2, 19]
The Custom elements repetition : [4, 6, 6, 7, 3, 3, 1, 1, 9, 2, 2, 19]
OutputThe Custom elements repetition : [4, 6, 6, 7, 3, 3, 1, 1, 9, 2, 2, 19]
OutputThe Custom elements repetition : [4, 6, 6, 7, 3, 3, 1, 1, 9, 2, 2, 19]
The time complexity of this approach is O(n), where n is the length of the input list, because we iterate over each element of the list once.
The auxiliary space is also O(n), because we create a new list with the same length as the input list to store the results.
OutputThe Custom elements repetition : [4, 6, 6, 7, 3, 3, 1, 1, 9, 2, 2, 19]