next up previous contents
Next: The in operator Up: List Operators Previous: Concatenation   Contents


Repetition

Like strings, the asterisk (*) is overloaded for lists to serve as a repetition operator. The result of applying repetition to a list is a single list, with the elements of the original list repeated as many times as you specify:
>>> ['a','b','c'] * 4
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
Special care needs to be taken when the list being repeated contains only one element. Note that the expression 3 * 5 is a simple numeric computation, while
>>> 3 * [5]
[5, 5, 5]
produces a list containing the element 5 repeated 3 times. By surrounding a list to be repeated with an extra set of square brackets, lists consisting of other lists can be constructed:
>>> littlelist = [1,2,3]
>>> 3 * littlelist
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> 3 * [littlelist]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]



Phil Spector 2003-11-12