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

Joseph Jevnik joejev at gmail.com
Wed Aug 5 02:43:12 CEST 2015


Iterators all all different types though. iter(list) returns a
list_iterator type, iter(dict.keys()) returns a dict_keys_iterator type and
so on. Is your suggestion that the standard lib types do this? How do we
update all of the existing iterators not in the stdlib that do not do this?
Finally, how is this better than itertools.chain?

On Tue, Aug 4, 2015 at 8:22 PM, Grayson, Samuel Andrew <
sag150430 at utdallas.edu> wrote:

> 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150804/51811014/attachment.html>


More information about the Python-ideas mailing list