What I have:
string = "string"
range_list = list(range(10))
What I want:
['string0',
'string1',
'string2',
'string3',
'string4',
'string5',
'string6',
'string7',
'string8',
'string9']
What I usually do:
import pandas as pd
(string pd.Series(range_list).astype(str)).tolist()
What I would like to do:
obtain the same expected output from the same input, without importing libraries nor using loops
Since there is probably no way to do this complying my requests, any other solution cleaner and/or more performing than mine is well accepted. Thanks in advice.
CodePudding user response:
You can do this using list comprehension and f-string.
[f"{string}{idx}" for idx in range_list]
CodePudding user response:
This works too
string = "string"
str_list = [string str(i) for i in range(10)]
CodePudding user response:
You can use map
with a function or a lambda to avoid using a loop.
def get_string(x):
return f'string{x}'
list(map(get_string, range(10)))
or with a lambda:
list(map(lambda x: f'string{x}', range(10)))