Avoiding the (explicit) nested loops:
-
sum([[x]*5 for x in range(1,n+1)],[])
(Per @StevenRumbalski’s suggestion, a better approach would beitertools.chain(*[[x]*5 for x in range(1,n+1)])
, which yields an iterable of the values you want.) -
[x for x in range(1,6)]*n
More Related Contents:
- Why is a list comprehension so much faster than appending to a list?
- Skip elements on a condition based in a list comprehension in python
- how to create a list of lists using list comprehension [closed]
- User input integer list [duplicate]
- how to unpack a tuple in this form (‘abc’, [20, 20])? [closed]
- What does “list comprehension” and similar mean? How does it work and how can I use it?
- Getting a map() to return a list in Python 3.x
- What do ellipsis […] mean in a list?
- Are lists thread-safe?
- List comprehension vs generator expression’s weird timeit results?
- Extending list returns None [duplicate]
- How can I generate a list of consecutive numbers?
- How to return a subset of a list that matches a condition [duplicate]
- Print list of lists in separate lines
- Comprehension on a nested iterables?
- remove None value from a list without removing the 0 value
- Best way to check if an item is present in a list of lists? [duplicate]
- How can I use list comprehensions to process a nested list?
- How to apply a function to each sublist of a list in python?
- Changing one dict changes all dicts in a list of dicts [duplicate]
- Appending to list in Python dictionary [duplicate]
- Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3?
- sum each value in a list of tuples
- Duplicate elements in a list [duplicate]
- Appending to a list comprehension in Python returns None [duplicate]
- Python – How to convert only numbers in a mixed list into float?
- Nested lambda statements when sorting lists
- Optimal method to find the max of sublist items within list
- Removing an item from a list of lists based on each of the lists first element
- How to convert only numbers in a mixed list into float