Python – Length Conditional Concatenation

17 Jun, 2022
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 = 3Output : 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 liststest_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything'] # printing original listprint("The original list : " + str(test_list)) # initializing KK = 2 # loop to run through all the elementsres = ''for ele in test_list:         # using len() to check for length    if len(ele) > 2:        res += ele # printing resultprint("String after Concatenation : " + str(res))
The original list : ['Gfg', 'is', 'Best', 'for', 'CS', 'Everything']
String after Concatenation : GfgBestforEverything

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 liststest_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything'] # printing original listprint("The original list : " + str(test_list)) # initializing KK = 2 # join() performing Concatenation of required stringsres = ''.join(filter(lambda ele: len(ele) > K, test_list)) # printing resultprint("String after Concatenation : " + str(res))
The original list : ['Gfg', 'is', 'Best', 'for', 'CS', 'Everything']
String after Concatenation : GfgBestforEverything

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 liststest_list = ["Gfg", 'is', "Best", 'for', 'CS', 'Everything'] # Printing original listprint("The original list : " + str(test_list)) # Initializing KK = 3 # list comprehension make list of string with greater length# join() performing Concatenation of required stringstemp = [x for x in test_list if len(x) > K]res = "".join(temp) # Printing resultprint("String after Concatenation : " + str(res))
The original list : ['Gfg', 'is', 'Best', 'for', 'CS', 'Everything']
String after Concatenation : BestEverything

satyam00so
Python list-programs
Python string-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers