Open In App

Python - Insert the string at the beginning of all items in a list

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, modifying list elements by adding a specific string to the beginning of each item is a common and useful task. By iterating through the list and appending the desired string to each element, we can create a new list with the updated values.

Using map()

map() function allows us to apply a specified operation to each item in an iterable. The operation can be a regular function or a lambda function. When combined with a lambda function, we can adds a string in front of each number by converting the number to a string to each item in the iterable.

Python
li = [1, 2, 3, 4]

s = 'Geek'

res = list(map(lambda x: s + str(x), li))
print(res)

Output
['Geek1', 'Geek2', 'Geek3', 'Geek4']

Explanation:

  • map() applies a function to each item in a list .
  • lambda x: s + str(x): This takes an item from li, turns it into a string, and adds the word 'Geek' before it.
  • list() is used to convert the iterator from map() into a list.

Using List comprehension

List comprehension is a efficient way to create a new list by modifying each item in an existing list. In this case, it adds a string before each number in the list, converting the number to a string. It's a more readable alternative to using regular loops.

Python
li = [1, 2, 3, 4]

s = 'Geek'

res = [s + str(i) for i in li]
print(res)

Output
['Geek1', 'Geek2', 'Geek3', 'Geek4']

Explanation:

  • for i in li: This iterates through each element of the list .
  • str(i): This converts the integer i to a string for concatenation.
  • s + str(i): This prepends the string s to the string representation of i.

Using string formatting

This method uses string formatting to prepend a string to each element in the list. By combining the string with the element using format(), it creates a new formatted string for each item.

Python
li = [1, 2, 3, 4]

s = 'Geek'

res = list(map(lambda x: '{}{}'.format(s, x), li))
print(res)

Output
['Geek1', 'Geek2', 'Geek3', 'Geek4']

Explanation:

  • map() : This applies the lambda function to each item in the list li.
  • format(): This formats each element by prepending 'Geek' to the element x.
  • {}{}: first {} is replaced by s string and second {} is replaced by x ,the current element of the list li .

Using for loop

By using a for loop, we can iterate through the list, process each item individually, prepend a specific string to it, and store the modified elements in a new list.

Python
li = [1, 2, 3, 4]
s = 'Geek'

# Initialize an empty list
res = []
for i in li:
    res.append(s + str(i))
print(res)

Output
['Geek1', 'Geek2', 'Geek3', 'Geek4']

Explanation:

  • for i in li: This iterates through each element i in `li`.
  • res.append(s + str(i)): Converts i to a string and prepends 'Geek', then appends it to res .

Similar Reads