Open In App

Python – Incremental Slice concatenation in String list

Given a String list, perform the task of the concatenation of strings by increasing the size of each string.

Examples:



Input       : test_list = ['gfg', 'for', 'all', 'geeks'], 
Output      : gfoallgeek 
Explanation : g, fo, all, geek -> concatenated from each string [ increasing order ].
Input       : test_list = ['gfg', 'for', 'geeks'], 
Output      : gfogee 
Explanation : g, fo, gee -> concatenated from each string [ increasing order ]. 

Method #1 : Using loop + slicing 

In this, using loop, each string is iterated and concatenated by slicing increasing the counter at each pass.




# Python3 code to demonstrate working of
# Incremental Slice concatenation in String list
# Using loop + slicing
 
# initializing list
test_list = ['gfg', 'for', 'all', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
res = ''
for idx in range(len(test_list)):
 
    # Incremental slicing
    res += test_list[idx][:idx + 1]
 
# printing result
print("Incremental sliced concatenated string : " + str(res))

Output

The original list is : ['gfg', 'for', 'all', 'geeks']
Incremental sliced concatenated string : gfoallgeek

Time Complexity: O(n)
Space Complexity: O(n)

Method #2 : Using join() + list comprehension

In this, the task of concatenation is done using join(), the task of iteration is done using list comprehension to provide shorthand.




# Python3 code to demonstrate working of
# Incremental Slice concatenation in String list
# Using join() + list comprehension
 
# initializing list
test_list = ['gfg', 'for', 'all', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
# join performs concatenation
res = ''.join([test_list[idx][:idx + 1] for idx in range(len(test_list))])
 
# printing result
print("Incremental sliced concatenated string : " + str(res))

Output
The original list is : ['gfg', 'for', 'all', 'geeks']
Incremental sliced concatenated string : gfoallgeek

Time Complexity: O(n)
Space Complexity: O(n)

Method#3: Using reduce + List.index()

This is another way to perform this task. We can use reduce to iterate over the list and combine the slice string. We use List.index() to get the incremental value which is used in slicing of string. 




# Python3 code to demonstrate working of
# Incremental Slice concatenation in String list
# Using reduce + List.index()
 
import functools
# initializing list
lis = ['gfg', 'for', 'all', 'geeks']
 
# printing original list
print("The original list is : " + str(lis))
 
 
# Using reduce and list.index function for
# Incremental slice concatenate in string list
ans = functools.reduce(lambda a, b: a+b[:lis.index(b)+1], lis, "")
 
# printing result
print("Incremental sliced concatenated string : " + ans)

Output
The original list is : ['gfg', 'for', 'all', 'geeks']
Incremental sliced concatenated string : gfoallgeek

Time Complexity: O(n) 
Auxiliary Space: O(n)

Method 4: Use the map function and a lambda function.

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Incremental Slice concatenation in String list
# Using map() + lambda
 
# initializing list
test_list = ['gfg', 'for', 'all', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
# defining lambda function
func = lambda s, i: s[:i+1]
 
# using map() to perform slice and store in list
res_list = list(map(func, test_list, range(len(test_list))))
 
# join performs concatenation
res = ''.join(res_list)
 
# printing result
print("Incremental sliced concatenated string : " + str(res))

Output
The original list is : ['gfg', 'for', 'all', 'geeks']
Incremental sliced concatenated string : gfoallgeek

Time complexity: O(n^2) because it uses the map function and a lambda function, both of which have a time complexity of O(n). 
Auxiliary space: O(n), because it creates a new list of substrings using the map function, which has a space complexity of O(n), and a new string for the concatenated result, which has a space complexity of O(n).


Article Tags :