Python – All possible concatenations in String List
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 : 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().
The original list : ['Gfg', 'is', 'Best'] All String combinations : ['Gfg', 'is', 'Best', 'Gfgis', 'GfgBest', 'isGfg', 'isBest', 'BestGfg', 'Bestis', 'GfgisBest', 'GfgBestis', 'isGfgBest', 'isBestGfg', 'BestGfgis', 'BestisGfg']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course