ManyIterables

class iteration_utilities.ManyIterables(*iterables)

ManyIterables stores several iterables and implements methods to convert these to one Iterable.

Warning

ManyIterables itself cannot be iterated!

Parameters:

*iterables : any amount of iterables

The iterables to store.

Notes

This is just a convenience class to seperate the expressions dealing with multiple iterables from those applying on one.

Available methods:

Method Reference
chain() See itertools.chain().
map() See map().
merge() See merge().
product() See itertools.product().
roundrobin() See roundrobin().
zip() See zip().
zip_longest() See itertools.zip_longest().

Examples

Depending on the function and the types of the iterables the returned class may be different. For example map() returns an InfiniteIterable if all iterables are infinite:

>>> from iteration_utilities import ManyIterables
>>> ManyIterables(Iterable.from_count(10), range(10)).map(pow)  
<Iterable: <map object at ...>>

>>> ManyIterables(Iterable.from_count(10),
...               Iterable.from_count(10)).map(pow)  
<InfiniteIterable: <map object at ...>>

While other methods also return an InfiniteIterable if any of the iterables is infinite:

>>> ManyIterables(range(10), Iterable.from_count(10)).merge()  
<InfiniteIterable: <iteration_utilities.merge object at ...>>

>>> ManyIterables(range(10), range(10)).merge()  
<Iterable: <iteration_utilities.merge object at ...>>

Each method has a note explicitly stating to which of these categories it belongs.

chain()

See itertools.chain().

Note

If any of the iterables is infinite then this will also return an InfiniteIterable.

Examples

>>> from iteration_utilities import ManyIterables
>>> ManyIterables([1,3,5,7,9], [0,2,4,6,8]).chain().as_list()
[1, 3, 5, 7, 9, 0, 2, 4, 6, 8]
map(function)

See map().

Note

If all of the iterables is infinite then this will also return an InfiniteIterable.

Examples

>>> from iteration_utilities import ManyIterables
>>> ManyIterables([1,3,5,7,9], [0,2,4,6,8]).map(pow).as_list()
[1, 9, 625, 117649, 43046721]
merge(key=<default>, reverse=<default>)

See merge().

Note

If any of the iterables is infinite then this will also return an InfiniteIterable.

Examples

>>> from iteration_utilities import ManyIterables
>>> ManyIterables([1,3,5,7,9], [0,2,4,6,8]).merge().as_list()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from operator import neg
>>> ManyIterables([1,3,5,7,9], [0,2,4,6,8]).merge(neg, True).as_list()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> ManyIterables([1,3,5,7,9], [0,2,4,6,8]).merge(
...     key=neg, reverse=True).as_list()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
product(repeat=<default>)

See itertools.product().

Note

If any of the iterables is infinite then this will also return an InfiniteIterable.

Examples

>>> from iteration_utilities import ManyIterables
>>> ManyIterables([1,2], [10, 11, 12]).product().as_list()
[(1, 10), (1, 11), (1, 12), (2, 10), (2, 11), (2, 12)]
>>> ManyIterables([1], [10, 11]).product(2).as_list()
[(1, 10, 1, 10), (1, 10, 1, 11), (1, 11, 1, 10), (1, 11, 1, 11)]
>>> ManyIterables([1], [10, 11]).product(repeat=2).as_list()
[(1, 10, 1, 10), (1, 10, 1, 11), (1, 11, 1, 10), (1, 11, 1, 11)]
roundrobin()

See roundrobin().

Note

If any of the iterables is infinite then this will also return an InfiniteIterable.

Examples

>>> from iteration_utilities import ManyIterables
>>> ManyIterables([1,2,3,4], [10, 11, 12]).roundrobin().as_list()
[1, 10, 2, 11, 3, 12, 4]
zip()

See zip().

Note

If all of the iterables is infinite then this will also return an InfiniteIterable.

Examples

>>> from iteration_utilities import ManyIterables
>>> ManyIterables([1,2,3,4], [2,3,4,5]).zip().as_list()
[(1, 2), (2, 3), (3, 4), (4, 5)]
zip_longest(fillvalue=<default>)

See itertools.zip_longest().

Note

If any of the iterables is infinite then this will also return an InfiniteIterable.

Examples

>>> from iteration_utilities import ManyIterables
>>> ManyIterables([1,2,3,4], [2,3,4]).zip_longest().as_list()
[(1, 2), (2, 3), (3, 4), (4, None)]
>>> ManyIterables([1,2,3,4], [2,3,4]).zip_longest('x').as_list()
[(1, 2), (2, 3), (3, 4), (4, 'x')]
>>> ManyIterables([1,2,3,4], [2,3,4]).zip_longest(
...     fillvalue='x').as_list()
[(1, 2), (2, 3), (3, 4), (4, 'x')]