Home > Software engineering >  List that repeats based of numbers in original list
List that repeats based of numbers in original list

Time:10-02

From this list:

list=[3,4,1]

How do you get this list?:

desired_list=[3,3,3,4,4,4,4,1]

My attempt:

count=0
if count<list[count]:
    print(list[count])
    count =1

Thankyou

CodePudding user response:

You can iterate over lst then for range(element) repeat that element.

Try this:

>>> lst=[3,4,1]
>>> [l for l in lst for _ in range(l)]
[3, 3, 3, 4, 4, 4, 4, 1]

CodePudding user response:

I would recommend you read up on the basics of python (or any other language), e.g. for-loops and go from there.

Right now your code only executed count =1 once.

  • Related