Open In App

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 : Gfgbest 
Explanation : Combined as per order of indices. 

Method #1: Using loop



In this, we iterate order elements in the loop and perform concatenation of strings of the similar index in similar order.




# Python3 code to demonstrate working of
# Concatenate Strings in Order
# Using loop
 
# initializing list
test_list = ["best", "Gfg", "for", "is", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing join order
sort_order = [1, 3, 0, 2, 4]
 
res = ''
for order in sort_order:
     
    # concatenating by order
    res += test_list[order]
 
# printing result
print("Ordered concatenation : " + str(res))

Output
The original list is : ['best', 'Gfg', 'for', 'is', 'geeks']
Ordered concatenation : Gfgisbestforgeeks

Time Complexity: O(n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(1) additional space is not needed

Method #2: Using join() + list comprehension

In this, we perform the task of concatenation using join(), list comprehension is used for iteration of order.




# Python3 code to demonstrate working of
# Concatenate Strings in Order
# Using join() + list comprehension
 
# initializing list
test_list = ["best", "Gfg", "for", "is", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing join order
sort_order = [1, 3, 0, 2, 4]
 
# join() performs concatenation
res = ''.join([test_list[order] for order in sort_order])
 
# printing result
print("Ordered concatenation : " + str(res))

Output
The original list is : ['best', 'Gfg', 'for', 'is', 'geeks']
Ordered concatenation : Gfgisbestforgeeks

Time Complexity: O(n)
Space Complexity: O(n)

Method #3: Using map()

Here’s a different approach, which leverages the python map() function:




# Python3 code to demonstrate working of
# Concatenate Strings in Order
# Using map()
 
# initializing list
test_list = ["best", "Gfg", "for", "is", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing join order
sort_order = [1, 3, 0, 2, 4]
 
# using map() to get the concatenated string
res = ''.join(map(lambda x: test_list[x], sort_order))
 
# printing result
print("Ordered concatenation : " + str(res))

Output
The original list is : ['best', 'Gfg', 'for', 'is', 'geeks']
Ordered concatenation : Gfgisbestforgeeks

Time Complexity: O(n)
Auxiliary Space: O(n)

The code leverages the map() function to iterate over the sort_order list, and using a lambda function, it retrieves the corresponding string from the test_list. The join() function is then used to concatenate the retrieved strings into a single string.

Method 4: Using the zip() function and a generator expression

Step-by-step approach:




# Python3 code to demonstrate working of
# Concatenate Strings in Order
# Using zip() + generator expression
 
# initializing list
test_list = ["best", "Gfg", "for", "is", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing join order
sort_order = [1, 3, 0, 2, 4]
 
# using zip() to map values
# iterating over the generator expression
res = ''.join(test_list[i] for i in sort_order)
 
# printing result
print("Ordered concatenation : " + str(res))

Output
The original list is : ['best', 'Gfg', 'for', 'is', 'geeks']
Ordered concatenation : Gfgisbestforgeeks

Time complexity: O(n), where n is the length of the list and the sort_order array. Both of these are iterated over once.
Auxiliary space: O(1), since we only use constant extra space to store the intermediate result and no additional data structures are used

Method 5: Use the sorted() function with a custom key function. 

Step-by-step approach:




# Python3 code to demonstrate working of
# Concatenate Strings in Order
# Using sorted() function with custom key
 
# initializing list
test_list = ["best", "Gfg", "for", "is", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing join order
sort_order = [1, 3, 0, 2, 4]
 
# define a custom key function
def custom_key(item):
    return sort_order.index(test_list.index(item))
 
# using sorted() with custom key to concatenate strings in order
res = ''.join(sorted(test_list, key=custom_key))
 
# printing result
print("Ordered concatenation : " + str(res))

Output
The original list is : ['best', 'Gfg', 'for', 'is', 'geeks']
Ordered concatenation : Gfgisbestforgeeks

Time complexity: O(nlogn) due to the use of sorted(), 
Auxiliary space: O(n) for storing the sorted list of strings.


Article Tags :