Python - Concatenate Ranged Values in String list
                                        
                                                                                    
                                                
                                                    Last Updated : 
                                                    30 Mar, 2023
                                                
                                                 
                                                 
                                             
                                                                             
                                                             
                            
                            
                                                                                    
                
Given list of strings, perform concatenation of ranged values from the Strings list.
Input : test_list = ["abGFGcs", "cdforef", "asalloi"], i, j = 3, 5 
Output : FGorll 
Explanation : All string sliced, FG, or and ll from all three strings and concatenated. 
Input : test_list = ["aGFGcs", "cforef", "aalloi"], i, j = 1, 4 
Output : GFGforall 
Explanation : Similar slicing operation different ranges.
Method #1 : Using loop + string slicing
This is brute way in which this task can be performed. In this we iterate for all strings and perform concatenation of values of range of each string.
            Python3
    # Python3 code to demonstrate working of 
# Concatenate Ranged Values in String list
# Using loop
# initializing list
test_list = ["abGFGcs", "cdforef", "asalloi"]
# printing original list
print("The original list : " + str(test_list))
# initializing range
i, j = 2, 5
res = ''
for ele in test_list:
    
    # Concatenating required range
    res += ele[i : j]
        
# printing result 
print("The Concatenated String : " + str(res))
OutputThe original list : ['abGFGcs', 'cdforef', 'asalloi']
The Concatenated String : GFGforall
 
Time complexity: O(n * (j-i)), where n is the length of the input list and (j-i) is the length of the concatenated substring.
Auxiliary Space: O(j-i), as we are creating a new string of length (j-i) to store the concatenated substring. The space required for the input list and loop variables is not considered as auxiliary space as they are part of the program logic.
Method #2  : Using list comprehension + string slicing
This is yet another way in which this task can be performed. In this, we extract a particular range of string in one liner using list comprehension and string slicing as above method. 
            Python3
    # Python3 code to demonstrate working of 
# Concatenate Ranged Values in String list
# Using list comprehension + string slicing
# initializing list
test_list = ["abGFGcs", "cdforef", "asalloi"]
# printing original list
print("The original list : " + str(test_list))
# initializing range
i, j = 2, 5
# join() used to join slices together
res = ''.join([sub[i : j] for sub in test_list])
        
# printing result 
print("The Concatenated String : " + str(res))
OutputThe original list : ['abGFGcs', 'cdforef', 'asalloi']
The Concatenated String : GFGforall
 
Time complexity: O(n*m), where n is the length of the input list and m is the length of the substring (j-i).
Auxiliary space: O(m), where m is the length of the substring (j-i).
Method 3: using the map() function and the join() method. Here is an example code:
            Python3
    # Python3 code to demonstrate working of 
# Concatenate Ranged Values in String list
# Using map() function + string slicing
# initializing list
test_list = ["abGFGcs", "cdforef", "asalloi"]
# printing original list
print("The original list : " + str(test_list))
# initializing range
i, j = 2, 5
# using map() to perform slicing
sliced_list = map(lambda x: x[i:j], test_list)
# join() used to join slices together
res = ''.join(sliced_list)
# printing result
print("The Concatenated String : " + str(res))
OutputThe original list : ['abGFGcs', 'cdforef', 'asalloi']
The Concatenated String : GFGforall
 
Time complexity of the above code is O(nm), where n is the number of strings in the list and m is the length of the sliced string.
Auxiliary space complexity of the above code is O(nm), where n is the number of strings in the list and m is the length of the sliced string.
Method #4: Using the reduce() function from functools module.
Use reduce() to apply a lambda function that concatenates the required range of each string in the list. The lambda function takes two arguments x and y, which represent the accumulated result and the current element of the list being processed, respectively. The initial value of x is set to an empty string ('') and the required range is concatenated with x using the string slicing operation y[i:j]. The result of reduce() is the final concatenated string.
            Python3
    from functools import reduce
# initializing list
test_list = ["abGFGcs", "cdforef", "asalloi"]
# initializing range
i, j = 2, 5
# Using reduce() + string slicing + lambda
res = reduce(lambda x, y: x + y[i:j], test_list, '')
# printing original list
print("The original list : " + str(test_list))
# printing result 
print("The Concatenated String : " + str(res))
OutputThe original list : ['abGFGcs', 'cdforef', 'asalloi']
The Concatenated String : GFGforall
 
Time complexity: O(N*L), where N is the number of strings in the list and L is the length of the required range.
Auxiliary space: O(L), where L is the length of the required range.
Method #5: Using str.join() and list slicing
- Initialize a list of strings to be concatenated, e.g., test_list = ["abGFGcs", "cdforef", "asalloi"].
- Define the range of characters to be concatenated from each string, e.g., i, j = 2, 5.
- Initialize an empty string to hold the concatenated substring, e.g., res = ''.
- Loop through each string in the list.
- For each string, extract the range of characters defined by i and j using string slicing, e.g., ele[i:j].
- Concatenate the extracted substring to the result string res.
- Once all strings have been processed, print the final result string.
Python3# Python3 code to demonstrate working of 
# Concatenate Ranged Values in String list
# Using str.join() and list slicing
# initializing list
test_list = ["abGFGcs", "cdforef", "asalloi"]
# printing original list
print("The original list : " + str(test_list))
# initializing range
i, j = 2, 5
# Extracting required range using list slicing and joining using str.join()
res = ''.join([s[i:j] for s in test_list])
# printing result 
print("The Concatenated String : " + str(res))
OutputThe original list : ['abGFGcs', 'cdforef', 'asalloi']
The Concatenated String : GFGforall
 
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, due to the list comprehension
                                
                                
                            
                                                                                
                                                            
                                                    
                                                
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice