You are currently viewing Python Append Prefix to List of Strings

How to append a prefix to a List of String elements in Python? We can add a string prefix to all the elements present in the list using by iterating all the strings in the list and add a prefix to each string using append(). Besides this, you can also use List Comprehension and map() with the lambda function.

Advertisements

1. Quick Examples of Appending Prefix to List of Strings

Following are quick examples of adding a prefix to a list of strings.


# Quick Examples of Appending Prefix to List of Strings

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

prefix = 'Country - '

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

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

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

2. Append Prefix to List of Strings Using List Comprehension

By using list comprehension you can easily append string prefix to all list elements in Python. Here, we will iterate the list and append the prefix using the ‘+’ operator. Let’s see the syntax of using List Comprehension to add prefix to all strings in the list.


# Syntax
[prefix + iterator for iterator in list]

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


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

prefix = 'Country - '

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

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

3. Using for and append() to append Prefix to String List

We can also append string prefix to all elements of the list in Python by iterating using for loop and adding each element to the new list after adding a prefix. To append to a list, you can use the append() function. Let’s see the syntax of using for loop with append()


# Syntax
final=[]
for iterator in list1:
  final.append(prefix + iterator)

Here, list1 is the input list. Let’s create a list that holds 4 countries and append the prefix – ‘Country – ‘ to all these 4 countries.


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

prefix = 'Country - '

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

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

4. Using map()

Let’s use map() with lambda to add a prefix to each element of the list. map() takes two parameters, the first parameter is the lambda expression which will add prefix 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.

Let’s see the syntax of using map() with lambda expression.


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

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


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

prefix = 'Country - '

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

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

5. Conclusion

We have seen multiple ways to append string prefix 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 Article