Python - All possible concatenations in String List
Last Updated :
05 Apr, 2023
Sometimes, while working with String lists, we can have a problem in which we need to perform all possible concatenations of all the strings that occur in list. This kind of problem can occur in domains such as day-day programming and school programming.
Let's discuss a way in which this task can be performed.
Input : test_list = ['Gfg', 'Best']
Output : ['Gfg', 'Best', 'GfgBest', 'BestGfg']
Input : test_list = ['Gfg']
Output : ['Gfg']
Method 1: Using permutations() + join() + loop
The combination of above functions can be used to solve this problem. In this, we perform the task of concatenation using join() and all possible combination extraction using permutations().
Python3
# Python3 code to demonstrate working of
# All possible concatenations in String List
# Using permutations() + loop
from itertools import permutations
# initializing list
test_list = ['Gfg', 'is', 'Best']
# printing original list
print("The original list : " + str(test_list))
# All possible concatenations in String List
# Using permutations() + loop
temp = []
for idx in range(1, len(test_list) + 1):
temp.extend(list(permutations(test_list, idx)))
res = []
for ele in temp:
res.append("".join(ele))
# printing result
print("All String combinations : " + str(res))
OutputThe original list : ['Gfg', 'is', 'Best']
All String combinations : ['Gfg', 'is', 'Best', 'Gfgis', 'GfgBest', 'isGfg', 'isBest', 'BestGfg', 'Bestis', 'GfgisBest', 'GfgBestis', 'isGfgBest', 'isBestGfg', 'BestGfgis', 'BestisGfg']
Time complexity: O(n*n!) where n is the length of the input list.
Auxiliary Space: O(n*n!) where n is the length of the input list. The space complexity is dominated by the number of permutations that are generated and stored in the temporary list temp.
Method - 2: Using recursion and concatenation:
Approach:
- Define a helper function helper(lst, i, j) that takes the input list lst and two indices i and j and performs the following steps:
- If i is equal to the length of the list lst, then we have reached the end of the list and we return a list containing the string at index j.
- If j is equal to the length of the list lst, then we have reached the end of a pass through the list and need to move on to the next index i by calling the helper function again with i+1 and 0 as the new indices.
- If i and j are not equal, then we concatenate the strings at indices i and j and add the resulting string to a list, along with the results of a recursive call to the helper function with the same i and j+1 indices.
- If i and j are equal, then we simply call the helper function again with the same i and j+1 indices.
- Finally, we return the results of calling the helper function with initial indices 0 and 0, concatenated with the original input list.
Below is the implementation of the above approach:
Python3
# Python program for the above approach
# Function to generate all possible combination
# using recursion
def concat_list4(lst):
def helper(lst, i, j):
if i == len(lst):
return [lst[j]]
if j == len(lst):
return helper(lst, i+1, 0)
if i != j:
return [lst[i] + lst[j]] + helper(lst, i, j+1)
return helper(lst, i, j+1)
return helper(lst, 0, 0) + lst
# Driver Code
# Test case 1
test_list1 = ['Gfg', 'Best']
output1 = concat_list4(test_list1)
print(output1) # Output: ['Gfg', 'Best', 'GfgBest', 'BestGfg']
# Test case 2
test_list2 = ['Gfg']
output2 = concat_list4(test_list2)
print(output2) # Output: ['Gfg']
Output['GfgBest', 'BestGfg', 'Gfg', 'Gfg', 'Best']
['Gfg', 'Gfg']
Time Complexity: O(N2)
Space Complexity: O(N2)
Similar Reads
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
Python - All possible space joins in String Sometimes, while working with Python Strings, we can have a problem in which we need to construct strings with a single space at every possible word ending. This kind of application can occur in domains in which we need to perform testing. Let us discuss certain ways in which this task can be perfor
8 min read
Python - String concatenation in Heterogeneous list Sometimes, while working with Python, we can come across a problem in which we require to find the concatenation of strings. This problem is easier to solve. But this can get complex cases we have a mixture of data types to go along with it. Letâs discuss certain ways in which this task can be perfo
4 min read
Python | Find Mixed Combinations of string and list Sometimes, while working with Python, we can have a problem in which we need to make combinations of string and character list. This type of problem can come in domains in which we need to interleave the data. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + e
5 min read
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