![]() |
VOOZH | about |
Given a list and an integer N, write a Python program to increase the size of the list by padding each element by N.
Examples:
Input : lst = [1, 2, 3] N = 3 Output : [1, 1, 1, 2, 2, 2, 3, 3, 3] Input : lst = ['cats', 'dogs'] N = 2 Output : ['cats', 'cats', 'dogs', 'dogs']
Approach #1 : List comprehension
[1, 1, 1, 2, 2, 2, 3, 3, 3]
Time Complexity: O(n), where n is the number of elements in the list
Auxiliary Space: O(1), constant space needed
Approach #2 : Using functools.reduce() method The reduce function apply a particular function passed in its argument to all of the list elements. Therefore, in this approach we apply a function on each element where it's occurrence gets multiplied by N.
[1, 1, 1, 2, 2, 2, 3, 3, 3]
Approach #3 : Using itertools.chain()
[1, 1, 1, 2, 2, 2, 3, 3, 3]
Approach #4 : Using * and extend() method
[1, 1, 1, 2, 2, 2, 3, 3, 3]
Time Complexity : O(N)
Auxiliary Space : O(N)