 Open in App
Not now

# Python – Concatenate Strings in the Given Order

• Last Updated : 10 Mar, 2023

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

 `# 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

 `# 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```

The Time and Space Complexity for all the methods are the same:

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

Method #3: Using map()

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

## Python3

 `# 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.

My Personal Notes arrow_drop_up