Python: transform a list to a list of the list, by duplicating the values

Question:

maybe a simple question, but I would like to transform a list to a list of the list, by duplicating the values. What is the most efficient way to do it? Many thanks!

The following

data = ['a','b','c']

should look like:

data = [
    ['a','a','a'], 
    ['b','b','b'], 
    ['c','c','c'], 
]
Asked By: user7665853

||

Answers:

You can use List Comprehensions:

data = ['a','b','c']
rep = 3
out = [[d]*rep for d in data]
print(out)

[['a', 'a', 'a'], ['b', 'b', 'b'], ['c', 'c', 'c']]
Answered By: I'mahdi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.