I need to create this list ["1", "12", "123", "1234", "12345", ..., "123456789"] without using loops. How can I do it by using map, lambda functions?
I can do it like in the code writthen below, but that's not what I need:
lst = []
num = ""
for i in range(1,10):
num += str(i)
lst.append(num)
print(lst)
I also tried this: lst = list(map(lambda x: str(x), range(1,10))) , but I got ['1', '2', '3', '4', '5', '6', '7', '8', '9'] instead of ["1", "12", "123", "1234", "12345", ..., "123456789"].
Your attempt of list(map(lambda x: str(x), range(1,10)))
is a good start, but in order to produce a string of incremental numbers joined together you can map another range
generator to str
and concatenate them with ''.join
:
list(map(lambda n: ''.join(map(str, range(1, n + 1))), range(1, 10)))