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 comprehension + enumerate() List comprehension with the help of enumerate function can be used to perform this particular task in which we use the tuple which increases its length every alternate element for the insertion using the enumerate function.
Python3
test_list = [ 1 , 2 , 3 , 4 , 5 ]
print ( "The original list : " + str (test_list))
res = [j for sub in ((i, '*' * k)
for k, i in enumerate (test_list, 1 ))
for j in sub]
print ( "The increasing element pattern list : " + str (res))
|
Output
The original list : [1, 2, 3, 4, 5]
The increasing element pattern list : [1, '*', 2, '**', 3, '***', 4, '****', 5, '*****']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using itertools.chain.from_iterable() + zip() This task can be performed efficiently using the above functions. The zip function interleaves the formed strings using string multiplication with the lists and from_iterable function does the task of flattening the obtained tuples.
Python3
import itertools
test_list = [ 1 , 2 , 3 , 4 , 5 ]
print ( "The original list : " + str (test_list))
res = list (itertools.chain.from_iterable(
zip (test_list, ( "*" * (i + 1 )
for i in range ( len (test_list))))))
print ( "The increasing element pattern list : " + str (res))
|
Output
The original list : [1, 2, 3, 4, 5]
The increasing element pattern list : [1, '*', 2, '**', 3, '***', 4, '****', 5, '*****']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using extend() and * operator
Python3
test_list = [ 1 , 2 , 3 , 4 , 5 ]
print ( "The original list : " + str (test_list))
res = []
for i in range ( 0 , len (test_list)):
x = [test_list[i], "*" * test_list[i]]
res.extend(x)
print ( "The increasing element pattern list : " + str (res))
|
Output
The original list : [1, 2, 3, 4, 5]
The increasing element pattern list : [1, '*', 2, '**', 3, '***', 4, '****', 5, '*****']
Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(n), because we are creating a new list res that has a length of 2n, where n is the length of the test_list.
Method #3 : Using repeat() and chain() from itertools
This code imports the chain() and repeat() functions from the itertools module. It then defines a function called increase_pattern() that takes in a list as an argument.
The function first uses the zip() function to pair the elements in the input list with their corresponding indices. It then uses a list comprehension to multiply each element by its corresponding index and create a list of tuples.
Next, the function uses the chain() function to flatten the list of tuples into a single list. It does this by passing the chain() function the result list as an argument, preceded by the * operator.
Python3
from itertools import chain, repeat
def increase_pattern(lst):
pairs = zip (lst, range ( 1 , len (lst) + 1 ))
result = [(elem, " ".join(repeat(" * ", index))) for elem, index in pairs]
result = list (chain( * result))
return result
test_list = [ 1 , 2 , 3 , 4 , 5 ]
print ( "The increasing element pattern list : " + str (increase_pattern(test_list)))
|
Output
The increasing element pattern list : [1, '*', 2, '**', 3, '***', 4, '****', 5, '*****']
Time complexity: O(n), where n is the length of the input list. This is because the function iterates over the input list once to create the pairs, and again to create the result list.
Auxiliary space: O(n), as the size of the output list is directly proportional to the size of the input list.
Method 4: Using a for loop and append()
- Initialize an empty list res to store the output
- Loop over each element elem and its index i in the list test_list using the enumerate function
- Append the current element elem to the result list res
- Append a string of i+1 asterisks to the result list res
- Use string multiplication to create the required number of asterisks, i.e., ‘*’ * (i+1)
- Looping through all the elements and asterisks, we get the required increasing alternate element pattern list.
- Print the final list res using the print statement.
Below is the implementation:
Python3
test_list = [ 1 , 2 , 3 , 4 , 5 ]
print ( "The original list : " + str (test_list))
res = []
for i, elem in enumerate (test_list):
res.append(elem)
res.append( '*' * (i + 1 ))
print ( "The increasing element pattern list : " + str (res))
|
Output
The original list : [1, 2, 3, 4, 5]
The increasing element pattern list : [1, '*', 2, '**', 3, '***', 4, '****', 5, '*****']
Time complexity: O(n), where n is the number of elements in the input list test_list. This is because we are iterating over each element in the list once and performing constant time operations.
Auxiliary space: O(n), since we are creating a list res of size 2n, where n is the number of elements in the input list test_list. This is because we are adding an asterisk string after each element in the list.
Method #6: Using map() and lambda function
In this method, we use the map() function along with a lambda function to iterate over the list and generate the resulting list. We use the lambda function to add stars to the list based on the index.
- Initialize a list named test_list with the values [1, 2, 3, 4, 5]
- Print the original list using the print() function
- Create a lambda function that takes an index i as input and returns a list containing the element at the ith index of test_list and a string of asterisks (*) of length (i+1).
- Use the map() function to apply the lambda function to each index in the test_list and create a list of lists.
- Convert the resulting list of lists into a flattened list using a list comprehension that iterates over each sublist and each element in each sublist.
- Print the resulting list using the print() function, with a message that describes the list as an “increasing element pattern list”.
- The resulting output should look like.
Python3
test_list = [ 1 , 2 , 3 , 4 , 5 ]
print ( "The original list : " + str (test_list))
res = list ( map ( lambda i: [test_list[i], '*' * (i + 1 )], range ( len (test_list))))
res = [elem for sublist in res for elem in sublist]
print ( "The increasing element pattern list : " + str (res))
|
Output
The original list : [1, 2, 3, 4, 5]
The increasing element pattern list : [1, '*', 2, '**', 3, '***', 4, '****', 5, '*****']
Time complexity: O(n)
Auxiliary space: O(n)
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...