# HG changeset patch # User Paul Boddie # Date 1491176226 -7200 # Node ID 88933212404b336de295219755231c9799a9b405 # Parent 4f77605e14e883a6036ecf99069b04fd311f433c Added support for list replication. diff -r 4f77605e14e8 -r 88933212404b lib/__builtins__/list.py --- a/lib/__builtins__/list.py Mon Apr 03 01:36:47 2017 +0200 +++ b/lib/__builtins__/list.py Mon Apr 03 01:37:06 2017 +0200 @@ -113,6 +113,27 @@ self.extend(other) return self + def __mul__(self, other): + + "Replicate this sequence 'other' times." + + return self._mul(list(self), other) + + def __imul__(self, other): + + "Replicate this list 'other' times." + + return self._mul(self, other) + + def _mul(self, l, other): + + "Replicate 'l' 'other' times." + + while other > 1: + l.extend(self) + other -= 1 + return l + def __str__(self): "Return a string representation." diff -r 4f77605e14e8 -r 88933212404b tests/list.py --- a/tests/list.py Mon Apr 03 01:36:47 2017 +0200 +++ b/tests/list.py Mon Apr 03 01:37:06 2017 +0200 @@ -57,6 +57,9 @@ c = a + b print c # [1, 2, 3, 4, 5, 6] +a2 = a * 2 +print a2 # [1, 2, 3, 4, 1, 2, 3, 4] + # Test removal. print c.pop() # 6