[Python-ideas] Use the plus operator to concatenate iterators

Grayson, Samuel Andrew sag150430 at utdallas.edu
Wed Aug 5 02:22:51 CEST 2015


Concatenation is the most fundamental operation that can be done on iterators. In fact, we already do that with lists.

    [1, 2, 3] + [4, 5, 6]
    # evaluates to [1, 2, 3, 4, 5, 6]

I propose:

    iter([1, 2, 3]) + iter([4, 5, 6])
    # evaluates to something like itertools.chain(iter([1, 2, 3]), iter([4, 5, 6]))
    # equivalent to iter([1, 2, 3, 4, 5, 6])

There is some python2 code where:

    a = dict(zip('abcd', range(4)))
    isinstance(a.values(), list)
    alphabet = a.keys() + a.values()

In python2, this `alphabet` becomes a list of all values and keys

In current python3, this raises:

    TypeError: unsupported operand type(s) for +: 'dict_keys' and 'dict_values'

But in my proposal, it works just fine. `alphabet` becomes an iterator over all values and keys (similar to the python2 case).

Sincerely,
Sam G
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150805/2a5cfe62/attachment.html>


More information about the Python-ideas mailing list