Prepend the same string to all items in a list
Use a list comprehension:
[f'hello{i}' for i in a]
A list comprehension lets you apply an expression to each element in a sequence. Here that expression is a formatted string literal, incorporating i
into a string starting with hello
.
Demo:
>>> a = [1,2,3,4]
>>> [f'hello{i}' for i in a]
['hello1', 'hello2', 'hello3', 'hello4']
One more option is to use built-in map function:
a = range(10)
map(lambda x: 'hello%i' % x, a)
Edit as per WolframH comment:
map('hello{0}'.format, a)