Issue
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'],
]
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.