Pythonice way to repeat element based on second element in list of lists

As the title stated any list comprehension or enumerate way to do this. Note, this not assignment, juts me trying to improve my coding skills. Thanks

arr_1 = [
  ['boot', 3],
  ['camp', 2],
  ['program', 0]
ls = []
for sublist in arr_1:
    str_to_rept = sublist[0]
    num_of_time = sublist[1]
    for i in range(num_of_time):

['boot', 'boot', 'boot', 'camp', 'camp']

It’s possible to do it in one line using a nested comprehension:

[w for word, num in arr_1 for w in [word]*num]

But, clear is better than clever so I’d recommend a loop instead (and to have more descriptive names than arr and ls):

words = []
for word, num in word_nums:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.