How to create the following lists with list comprehensions? [closed]

Avoiding the (explicit) nested loops:

  1. sum([[x]*5 for x in range(1,n+1)],[])
    (Per @StevenRumbalski’s suggestion, a better approach would be itertools.chain(*[[x]*5 for x in range(1,n+1)]), which yields an iterable of the values you want.)

  2. [x for x in range(1,6)]*n

Leave a Comment