Python | Increase list size by padding each element by N
Last Updated :
17 Apr, 2023
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
Python3
# Python3 program to increase list size
# by padding each element by N
def increaseSize(lst, N):
return [el for el in lst for _ in range(N)]
# Driver code
lst = [1, 2, 3]
N = 3
print(increaseSize(lst, N))
Output:[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.
Python3
# Python3 program to increase list size
# by padding each element by N
from functools import reduce
def increaseSize(lst, N):
return reduce(lambda x, y: x + y, [[el]*N for el in lst])
# Driver code
lst = [1, 2, 3]
N = 3
print(increaseSize(lst, N))
Output:[1, 1, 1, 2, 2, 2, 3, 3, 3]
Approach #3 : Using itertools.chain()
Python3
# Python3 program to increase list size
# by padding each element by N
from itertools import chain
def increaseSize(lst, N):
return list(chain(*([el]*N for el in lst)))
# Driver code
lst = [1, 2, 3]
N = 3
print(increaseSize(lst, N))
Output:[1, 1, 1, 2, 2, 2, 3, 3, 3]
Approach #4 : Using * and extend() method
- Initiate a for loop to access list elements
- Repeat each list element by using * operator
- And add repeated elements to output list(using extend())
- Display output list
Python3
# Python3 program to increase list size
# by padding each element by N
lst = [1, 2, 3]
N = 3
x=[]
for i in lst:
x.extend([i]*N)
print(x)
Output[1, 1, 1, 2, 2, 2, 3, 3, 3]
Time Complexity : O(N)
Auxiliary Space : O(N)
Similar Reads
Python - Optional padding in list elements Optional padding in list elements involves adding extra elements, such as zeros or placeholders, to a list to ensure it reaches a desired length. This technique is useful when working with fixed-size data structures or when aligning data in lists.Using List ComprehensionThis method pads each element
2 min read
Python | Increasing alternate element pattern in list This particular article solves a very specific issue in which we need to insert every alternate element as the increased size pattern of repeated element to form a pattern. This can have a utility in demonstrative projects. Let's discuss certain ways in which this can be done. Method #1 : Using list
7 min read
Python | Return new list on element insertion The usual append method adds the new element in the original sequence and does not return any value. But sometimes we require to have a new list each time we add a new element to the list. This kind of problem is common in web development. Let's discuss certain ways in which this task can be perform
5 min read
Update Each Element in Tuple List - Python The task of updating each element in a tuple list in Python involves adding a specific value to every element within each tuple in a list of tuples. This is commonly needed when adjusting numerical data stored in a structured format like tuples inside lists. For example, given a = [(1, 3, 4), (2, 4,
3 min read
Python | K length Padding in List In real world problems, we sometimes require to pad the element of list according to a condition that maximum characters have reached. Padding a number with 0 if itâs length is less than required by any field is one of the basic issues that occur in web forms in Web Development. Letâs discuss certai
8 min read
Python - Create List of Size n Creating a list of size n in Python can be done in several ways. The easiest and most efficient way to create a list of size n is by multiplying a list. This method works well if we want to fill the list with a default value, like 0 or None.Python# Size of the list n = 5 # Creating a list of size n
2 min read