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

PythonServer Side ProgrammingProgramming

In this post we need to enter the string at the beginning of all items in a list. 

For ex: We're given string = "Tutorials_Point" and List contains multiple element such as "1", "2" etc. So in this we need to add Tutorials_Point in front of "1", "2" and so on.

Example

Aproach 1

 Live Demo

sample_list = [1, 2, 3]
print(['Tutorials_Point{0}'.format(i) for i in sample_list])

Output

//['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3']

Approach 2

 Live Demo

sample_list = [1, 2, 3]
sample_str = 'Tutorials_Point'
sample_str += '{0}'
sample_list = ((map(sample_str.format, sample_list)))
print(sample_list)

Output

//['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3']
raja
Published on 16-May-2020 10:49:49
Advertisements