Python - Length Conditional Concatenation
Last Updated :
09 Apr, 2023
Given a list of strings, perform concatenation of Strings whose length is greater than K.
Input : test_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything'], K = 3
Output : BestEverything
Explanation : All elements with Length > 3 are concatenated.
Input : test_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything'], K = 1
Output : GfgisBestforCSEverything
Explanation : All elements with Length > 1 are concatenated.
Method #1: Using loop + len():
This offers a brute way to solve this problem. In this, we iterate for each string and perform concatenation if the string length is greater than K using len().
Python3
# Python3 code to demonstrate working of
# Length Conditional Concatenation
# Using loop + len()
# initializing lists
test_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything']
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 2
# loop to run through all the elements
res = ''
for ele in test_list:
# using len() to check for length
if len(ele) > 2:
res += ele
# printing result
print("String after Concatenation : " + str(res))
OutputThe original list : ['Gfg', 'is', 'Best', 'for', 'CS', 'Everything']
String after Concatenation : GfgBestforEverything
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using join() + filter() + lambda + len():
The combination of above functions can be used to solve this problem. In this, we perform concatenation using join(), filter and lambda are used for conditional check using len().
Python3
# Python3 code to demonstrate working of
# Length Conditional Concatenation
# Using join() + filter() + lambda + len()
# initializing lists
test_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything']
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 2
# join() performing Concatenation of required strings
res = ''.join(filter(lambda ele: len(ele) > K, test_list))
# printing result
print("String after Concatenation : " + str(res))
OutputThe original list : ['Gfg', 'is', 'Best', 'for', 'CS', 'Everything']
String after Concatenation : GfgBestforEverything
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#3: Using list comprehension + join():
This method lists strings whose length is greater than the defined number. With the help of join method, we can join the list in string.
Python3
# Python3 code to demonstrate working of
# Length Conditional Concatenation
# Using list comprehension + join
# Initializing lists
test_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything']
# Printing original list
print("The original list : " + str(test_list))
# Initializing K
K = 3
# list comprehension make list of string with greater length
# join() performing Concatenation of required strings
temp = [x for x in test_list if len(x) > K]
res = "".join(temp)
# Printing result
print("String after Concatenation : " + str(res))
OutputThe original list : ['Gfg', 'is', 'Best', 'for', 'CS', 'Everything']
String after Concatenation : BestEverything
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Approach#4: Using reduce
This approach defines a function concat_strings that takes a list test_list and an integer K as input. It uses the filter function with a lambda function to filter the elements of test_list with length greater than K. It then uses the reduce function with a lambda function to concatenate the filtered elements. Finally, it returns the concatenated string.
Algorithm
1. Use reduce function to concatenate the elements of the input list with length greater than K.
2. Return the concatenated string.
Python3
from functools import reduce
def concat_strings(test_list, K):
filtered_list = filter(lambda string: len(string) > K, test_list)
concatenated_string = reduce(lambda x, y: x + y, filtered_list, '')
return concatenated_string
test_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything']
K=3
print(concat_strings(test_list, K))
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 creation of a new iterator).
Similar Reads
Python | Consecutive prefix overlap concatenation Sometimes, while working with Python Strings, we can have application in which we need to perform the concatenation of all elements in String list. This can be tricky in cases we need to overlap suffix of current element with prefix of next in case of a match. Lets discuss certain ways in which this
5 min read
Python - Horizontal Concatenation of Multiline Strings Horizontal concatenation of multiline strings involves merging corresponding lines from multiple strings side by side using methods like splitlines() and zip(). Tools like itertools.zip_longest() help handle unequal lengths by filling missing values, and list comprehensions format the result.Using z
3 min read
Python - String concatenation in Heterogeneous list 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 perfo
4 min read
Python | Records List Concatenation Sometimes, while working with Python records, we can have a problem in which we need to perform cross concatenation of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension +
4 min read
Python - Vertical Concatenation in Matrix Given a String Matrix, perform column-wise concatenation of strings, handling variable lists lengths. Input : [["Gfg", "good"], ["is", "for"]] Output : ['Gfgis', 'goodfor'] Explanation : Column wise concatenated Strings, "Gfg" concatenated with "is", and so on. Input : [["Gfg", "good", "geeks"], ["i
3 min read