Python - Incremental Slice concatenation in String list
Last Updated :
17 Apr, 2023
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))
OutputThe 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))
OutputThe 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)
OutputThe 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:
- 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', '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))
OutputThe 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).
Similar Reads
Python - String Matrix Concatenation Sometimes, while working with Matrix we can have a problem in which we have Strings and we need a universal concatenation of all the String present in it. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + join() We can solve this problem using lis
4 min read
Python | Concatenate N consecutive elements in String list Sometimes, while working with data, we can have a problem in which we need to perform the concatenation of N consecutive Strings in a list of Strings. This can have many applications across domains. Let's discuss certain ways in which this task can be performed. Method #1: Using format() + zip() + i
8 min read
Python - Incremental Size Chunks from Strings Given a String, split it into incremental sizes consecutive list. Input : test_str = 'geekforgeeks is best' Output : ['g', 'ee', 'kfo', 'rgee', 'ks is', ' best'] Explanation : Characters size increasing in list. Input : test_str = 'geekforgeeks' Output : ['g', 'ee', 'kfo', 'rgee', 'ks'] Explanation
3 min read
Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t
2 min read
Python - Concatenate Ranged Values in String list Given list of strings, perform concatenation of ranged values from the Strings list. Input : test_list = ["abGFGcs", "cdforef", "asalloi"], i, j = 3, 5 Output : FGorll Explanation : All string sliced, FG, or and ll from all three strings and concatenated. Input : test_list = ["aGFGcs", "cforef", "aa
5 min read
Python | Incremental slice partition in list Sometimes, while working with lists, we can come across a problem in which we need to slice a list incrementally, i.e., with each slice, the number of elements increases by 1. This has applications in competitive programming. Let's discuss certain ways in which this task can be performed. Method #1:
4 min read
Python | Split flatten String List Sometimes, while working with Python Strings, we can have problem in which we need to perform the split of strings on a particular deliminator. In this, we might need to flatten this to a single String List. Let's discuss certain ways in which this task can be performed. Method #1 : Using list compr
7 min read
Concatenate all Elements of a List into a String - Python We are given a list of words and our task is to concatenate all the elements into a single string with spaces in between. For example, given the list: li = ['hello', 'geek', 'have', 'a', 'geeky', 'day'] after concatenation, the result will be: "hello geek have a geeky day".Using str.join()str.join()
2 min read
Most efficient way to Concatenate Strings in Python Concatenation is an operation that is very frequently used in various problems related to strings. There are multiple methods to concat strings in various languages. Python is also such a language that supports various string concatenation methods. But have you ever wondered which one is the most ef
3 min read
Python - Concatenate Strings in the Given Order Given a String List and order list, perform string concatenation in a specific order. Input : test_list = ["best", "Gfg", "for", "is"], sort_order = [1, 3, 0, 2] Output : Gfgisbestfor Explanation : Combined as per order of indices. Input : test_list = ["best", "Gfg"], sort_order = [1, 0] Output : Gf
5 min read