Sometimes, while working with Python, we can come across a problem in which we require to find the concatenation of strings. This problem is easier to solve. But this can get complex cases we have a mixture of data types to go along with it. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + conditions
We can employ a brute force method to type caste check each element, if it’s a string we concatenate it. This can ensure that only strings are concatenated and hence can solve the problem.
Python3
# Python3 code to demonstrate working of # String concatenation in Heterogeneous list # using loop + conditions # initializing list test_list = [ 5 , 6 , "gfg " , 8 , ( 5 , 7 ), ' is' , 9 , ' best' ] # printing original list print ( "The original list is : " + str (test_list)) # String concatenation in Heterogeneous list # using loop + conditions res = '' for ele in test_list: if type (ele) = = str : res + = ele # printing result print ( "Concatenation of strings in list : " + str (res)) |
The original list is : [5, 6, 'gfg ', 8, (5, 7), ' is', 9, ' best'] Concatenation of strings in list : gfg is best
Time Complexity: O(n*n) where n is the number of elements in the string list. The loop + conditions is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.
Method #2 : Using join() + isinstance()
This problem can also be solved using the inbuilt function of join() and it also supports the instance filter using isinstance() which can be fed with strings and hence solve the problem.
Python3
# Python3 code to demonstrate working of # String concatenation in Heterogeneous list # using join() + isinstance() # initializing list test_list = [ 5 , 6 , "gfg " , 8 , ( 5 , 7 ), ' is' , 9 , ' best' ] # printing original list print ( "The original list is : " + str (test_list)) # String concatenation in Heterogeneous list # using join() + isinstance() res = "".join( filter ( lambda i: isinstance (i, str ), test_list)) # printing result print ( "Concatenation of strings in list : " + str (res)) |
The original list is : [5, 6, 'gfg ', 8, (5, 7), ' is', 9, ' best'] Concatenation of strings in list : gfg is best
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method#3: Using reduce + type
This problem can be solved using reduce and type function. We use reduce for iteration over the list and type is used to check the type of element in iteration if it is string type then add it in our result else leave it.
Python3
# Python3 code to demonstrate working of # String concatenation in Heterogeneous list # using reduce + type from functools import reduce # initializing list test_list = [ 5 , 6 , "gfg " , 8 , ( 5 , 7 ), ' is' , 9 , ' best' ] # printing original list print ( "The original list is : " + str (test_list)) # String concatenation in Heterogeneous list # using reduce + type() ans = reduce ( lambda x, y: x + y if type (y) = = str else x, test_list, "") # printing result print ( "Concatenation of strings in list : " + ans) |
The original list is : [5, 6, 'gfg ', 8, (5, 7), ' is', 9, ' best'] Concatenation of strings in list : gfg is best
Method #4: Using list comprehension
Step-by-step approach:
- Initialize the list test_list with heterogeneous data types.
- Print the original list.
- Use list comprehension to iterate over each element of test_list and return the element only if it is an instance of str.
- Concatenate the filtered elements using join() method and store it in the variable res.
- Print the concatenated string.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of # String concatenation in Heterogeneous list # using list comprehension # initializing list test_list = [ 5 , 6 , "gfg " , 8 , ( 5 , 7 ), ' is' , 9 , ' best' ] # printing original list print ( "The original list is : " + str (test_list)) # String concatenation in Heterogeneous list # using list comprehension res = ''.join([ele for ele in test_list if isinstance (ele, str )]) # printing result print ( "Concatenation of strings in list : " + str (res)) |
The original list is : [5, 6, 'gfg ', 8, (5, 7), ' is', 9, ' best'] Concatenation of strings in list : gfg is best
Time complexity: O(n)
Auxiliary space: O(n)