Skip to content
Related Articles

Related Articles

Python – Incremental Slice concatenation in String list

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Aug, 2022

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




# 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




# 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




# 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) (index + functools.reduce)

Space Complexity: O(n)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!