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', 'neveropen'], Output : gfoallgeek Explanation : g, fo, all, geek -> concatenated from each string [ increasing order ].
Input : test_list = ['gfg', 'for', 'neveropen'], 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
# Python3 code to demonstrate working of # Incremental Slice concatenation in String list # Using loop + slicing # initializing list test_list = [ 'gfg' , 'for' , 'all' , 'neveropen' ] # 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)) |
The original list is : ['gfg', 'for', 'all', 'neveropen'] 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
# Python3 code to demonstrate working of # Incremental Slice concatenation in String list # Using join() + list comprehension # initializing list test_list = [ 'gfg' , 'for' , 'all' , 'neveropen' ] # 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)) |
The original list is : ['gfg', 'for', 'all', 'neveropen'] 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
# Python3 code to demonstrate working of # Incremental Slice concatenation in String list # Using reduce + List.index() import functools # initializing list lis = [ 'gfg' , 'for' , 'all' , 'neveropen' ] # 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) |
The original list is : ['gfg', 'for', 'all', 'neveropen'] 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:
- Define a lambda function that takes a string and an index as arguments. The lambda function slices the string up to the index of the current iteration + 1, and returns the resulting substring.
- Use the map function to apply the lambda function to all the strings in the list. The map function returns a map object, which can be converted to a list.
- Use the join function to concatenate all the substrings in the resulting list.
- The resulting string contains the incremental slice concatenation of all the strings.
- Print the resulting string.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of # Incremental Slice concatenation in String list # Using map() + lambda # initializing list test_list = [ 'gfg' , 'for' , 'all' , 'neveropen' ] # 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)) |
The original list is : ['gfg', 'for', 'all', 'neveropen'] 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).