Python – Conditional String Append
Sometimes, while working with data, we have a problem in which we need to perform an append operation in a string on a particular condition. This kind of problem is common in web development and day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop This is a brute-force way to perform this task. In this, we run a loop and check for the condition, and according to that append the string to the original string.
Python3
# Python3 code to demonstrate# Conditional String Append# using loopdef append_str(item, boy_str, girl_str): if len(item) > 4 and item[-5] == ' ': return item + girl_str return item + boy_str # initializing listtest_list = ['Manjeet Singh', 'Harsimran Kaur', 'Sarbjeet Kaur']# initializing append stringboy_str = " Boy"girl_str = " Girl"# printing original listprint ("The original list is : " + str(test_list))# Conditional String Append# using loopres = [append_str(item, boy_str, girl_str) for item in test_list]# printing resultprint ("The filtered strings are : " + str(res)) |
The original list is : ['Manjeet Singh', 'Harsimran Kaur', 'Sarbjeet Kaur'] The filtered strings are : ['Manjeet Singh Boy', 'Harsimran Kaur Girl', 'Sarbjeet Kaur Girl']
Method #2: Using list comprehension List comprehension is shorthand for the longer method of loops. This solved problem in similar way but in shorter constructs.
Python3
# Python3 code to demonstrate# Conditional String Append# using list comprehension# initializing listtest_list = ['Manjeet Singh', 'Harsimran Kaur', 'Sarbjeet Kaur']# initializing append stringboy_str = " Boy"girl_str = " Girl"# printing original listprint ("The original list is : " + str(test_list))# Conditional String Append# using list comprehensionres = [ele + girl_str if ele[-5] == ' ' else ele + boy_str for ele in test_list]# printing resultprint ("The filtered strings are : " + str(res)) |
The original list is : ['Manjeet Singh', 'Harsimran Kaur', 'Sarbjeet Kaur'] The filtered strings are : ['Manjeet Singh Boy', 'Harsimran Kaur Girl', 'Sarbjeet Kaur Girl']
Method #3: Using map function Map function is used to create a new list by updating the existing list. We solve this problem by defining the condition append function for the map function which will apply to all the elements of the list.
Python3
# # Python3 code to demonstrate# Reverse Row sort in Lists of List# using map# Initializing append stringboy_str = " Boy"girl_str = " Girl"# Function to append the stringdef func(x): if x[-5] == ' ': return x + girl_str else: return x + boy_str# initializing listtest_list = ['Manjeet Singh', 'Harsimran Kaur', 'Sarbjeet Kaur']# printing original listprint("The original list is : " + str(test_list))# Conditinal string append using mapres = list(map(func, test_list))# printing resultprint("The filtered string are : " + str(res)) |
The original list is : ['Manjeet Singh', 'Harsimran Kaur', 'Sarbjeet Kaur'] The filtered string are : ['Manjeet Singh Boy', 'Harsimran Kaur Girl', 'Sarbjeet Kaur Girl']


