Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

Assume I have the list=[1,2,3,4] then I want to make the list of strings which length of each string associated with the corresponding value in a list.

It means a final output should be like this:

strs=['1', '11', '111', '1111']

I tried a code below but I am not sure how to continue.

lis=[1,2,3,4]

strs=[]

for i in range (len(lis)):

    st=lis[i]

    strs.append(st)

1 Answer

0 votes
by (36.8k points)

The multiplication for the strings is repetition, so:

lst=[1,2,3,4]

result = []

for e in lst:

    result.append('1'*e)

print(result)

Output

['1', '11', '111', '1111']

Want to be a Data Science expert? Come and join this Data Science Courses 

Related questions

Browse Categories

...