sequence multiplied by -1

Stefan Schwarzer sschwarzer at sschwarzer.net
Sat Sep 25 15:54:49 EDT 2010


Hi Terry,

On 2010-09-25 19:24, Terry Reedy wrote:
> On 9/25/2010 4:22 AM, Yingjie Lan wrote:
> There is already a builtin reversed() function whose output can be 
> multiplied.

Seemingly, it can't:

  $ python
  Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
  [GCC 4.4.3] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> L = [1, 2, 3]
  >>> 3 * reversed(L)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: unsupported operand type(s) for *: 'int' and 'listreverseiterator'

  $ python3
  Python 3.1.2 (r312:79147, Apr 15 2010, 12:35:07)
  [GCC 4.4.3] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> L = [1, 2, 3]
  >>> 3 * reversed(L)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: unsupported operand type(s) for *: 'int' and 'list_reverseiterator'

You can convert the result of `reversed` to a list though:

  >>> 3 * list(reversed(L))
  [3, 2, 1, 3, 2, 1, 3, 2, 1]

Stefan



More information about the Python-list mailing list