Python - All possible concatenations in String List Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report See More 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) Create Quiz Comment M manjeet_04 Follow 1 Improve M manjeet_04 Follow 1 Improve Article Tags : Python Python Programs Python list-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like