Reputation: 45
I need to repeat a certain set of items inside an array.
I need something like this in Python:
["a","b"] * 3
# result: ["a","b","a","b","a","b"]
I tried doing the same way, but I am getting:
(erb):329:in `*': Array can't be coerced into Integer (TypeError)
Is there an easy way to solve it?
Edit: Already solved. It seems to happen that it works when it is done like this
["a", "b"] * 2
But does not work backwards:
2 * ["a", "b"]
Upvotes: 0
Views: 73
Reputation: 54223
3 * 4
means:
3
repeated4
times.
4 * 3
means:
4
repeated3
times.
["a", "b"] * 3
means:
["a", "b"]
repeated3
times.
But what would
3
repeated["a", "b"]
times.
mean? How can you do anything ["a", "b"]
times?
In Python, both are allowed:
["a","b"] * 3
3 * ["a","b"]
and return:
['a', 'b', 'a', 'b', 'a', 'b']
but the second looks sloppy IMHO.
Upvotes: 2