You are currently viewing Python Append Suffix to List of Strings

How do append suffixes to all strings in the python list? We can append a string suffix to all elements of the list using for loop by iterating all the elements in the list and using the append method to add a suffix to each element.

Advertisements

Besides looping & using the append() method, you can also use List Comprehension and map() with lambda we can append suffixes to all the strings present in the list.

1. Quick Examples of Appending Suffix to Strings in List

These are quick examples to give you a high-level idea of how to append suffixes to all elements in List. We will see each method in detail in the upcoming section.


# Quick Examples of Appending Suffix to Strings in List

# Initialize list
countries = ['China','India','Japan','Russia']

# Suffix to append
suffix = '- Country'

# Using List Comprehension
countries = [i + suffix for i in countries]
print("Final countries: ",countries)

# Using map and lambda functions
countries = list(map(lambda x: x + suffix, countries))
print("Final countries: ",countries)

# Using for loop
final = []
for i in countries:
  final.append(i + suffix)
print("Final countries: ",final)

2. Append Suffix to All Strings in Python List using List Comprehension

To append suffix to all strings or elements in a Python list use the list comprehension. List comprehension is useful when working with large amounts of data. It is also helpful when we need to perform operations on multiple elements in a list.

Here, we will iterate the list and add the string at the suffix of each element using the ‘+’ operator. Let’s see the syntax of using List Comprehension to append string suffix to all elements of the list.


# Syntax
[iterator  + suffix for iterator in list]

Let’s create a list that holds 4 countries and append the suffix – ‘- Country’ to all these 4 countries.


# Append Suffix to All Strings in Python List using List Comprehension

# Initialize list
countries = ['China','India','Japan','Russia']
print("Actual countries: ",countries)

suffix = '- Country'

# Using List Comprehension
countries = [i + suffix for i in countries]
print("Final countries: ",countries)

# Output:
# Actual countries:  ['China', 'India', 'Japan', 'Russia']
# Final countries:  ['China- Country', 'India- Country', 'Japan- Country', 'Russia- Country']

3. Append Suffic to All Elements of List

The list.append() is used to insert elements to the list. we will iterate the list using for loop and add each element to another list by appending a string at the suffix. Let’s see the syntax of using for loop to append suffixes to all strings in the list.


# Here, list1 is the input list
final=[]
for iterator in list1:
  final.append(iterator  + suffix)

Let’s create a list that holds 4 countries and append the suffix – ‘- Country’ to all these 4 countries.


# Initialize list
countries = ['China','India','Japan','Russia']
print("Actual countries: ",countries)

suffix = '- Country'

# Using for loop
final = []
for i in countries:
  final.append(i + suffix)
print("Final countries: ",final)

# Output:
# Actual countries:  ['China', 'India', 'Japan', 'Russia']
# Final countries:  ['China- Country', 'India- Country', 'Japan- Country', 'Russia- Country']

4. Using map()

Let’s use map() with lambda to add a suffix to each element of the list, map() takes two parameters. The first parameter is the lambda expression which will add suffix to each string in the list. The second parameter is the input list of strings. Let’s see the syntax of using map() with a lambda expression.


# Here, list1 is the input list
list(map(lambda x: x + suffix, list1 ))

Let’s create a list that holds 4 countries and append the suffix – ‘- Country’ to all these 4 countries using map() with the lambda expression.


# Initialize list
countries = ['China','India','Japan','Russia']
print("Actual countries: ",countries)

suffix = '- Country'

# Using map and lambda functions
countries = list(map(lambda x: x + suffix, countries))
print("Final countries: ",countries)

# Output:
# Actual countries:  ['China', 'India', 'Japan', 'Russia']
# Final countries:  ['China- Country', 'India- Country', 'Japan- Country', 'Russia- Country']

5. Conclusion

We have seen multiple ways to append string suffix to each element of the list in Python. For example, using the List comprehension, looping through each element with append(), map() with a lambda expression.

Related Articles