More simplified explanation of chain.from_iterable and chain() of itertools
Asked Answered
M

3

1

Can you give a more simplified explanation of these two methods chain() and chain.from_iterable from itertools?

I have searched the knowledge base and as well the python documentation but i got confused.

I am new to python that's why I am asking a more simplified explanation regarding these.

Thanks!

Meador answered 1/4, 2017 at 0:1 Comment(4)
Do you understand how generators work in python? If not, it's probably best to start there.Klatt
Have you tried writing some code? It usually helps me to understand things I've read. Come up with a minimal reproducible example to show what you don't understand.Linkous
No, like I have said earlier, I am ramping up to learn Python. What I read about generators is that is a simplified way to loop to create a list or a dictionary. You may correct me if I am wrong on this part.Meador
See also: https://mcmap.net/q/235569/-what-is-the-difference-between-chain-and-chain-from_iterable-in-itertools/610569Inartificial
L
3

You can chain sequences to make a single sequence:

>>> from itertools import chain

>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> list(chain(a, b))
[1, 2, 3, 'a', 'b', 'c']

If a and b are in another sequence, instead of having to unpack them and pass them to chain you can pass the whole sequence to from_iterable:

>>> c = [a, b]
>>> list(chain.from_iterable(c))
[1, 2, 3, 'a', 'b', 'c']

It creates a sequence by iterating over the sub-sequences of your main sequence. This is sometimes called flattening a list. If you want to flatten lists of lists of lists, you'll have to code that yourself. There are plenty of questions and answers about that on Stack Overflow.

Linkous answered 1/4, 2017 at 0:15 Comment(0)
L
0

We can learn about the difference between these two tools by looking at the docs.

def chain(*iterables):
    # chain('ABC', 'DEF') --> A B C D E F
    ...

def from_iterable(iterable):
    # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
    ...

The key difference is in the signatures and how they handle an iterable, which is something that can be iterated or looped over.

  • chain accepts iterables, such as "ABC", "DEF" or [1, 2, 3], [7, 8, 9].
  • chain.from_iterable accepts one iterable, often a nested iterable, e.g. "ABCDEF" or [1, 2, 3, 7, 8, 9]. This is helpful for a flattening nested iterables. See its direct implementation in the flatten tool found in the itertools recipes.
Life answered 25/8, 2017 at 5:12 Comment(0)
C
0

chain excepts one or more iterables as argument and returns iterator consists of each items from iterables

like below

import itertools
for x in chain([1,2,3],(4,5,6)): #note can also accept different data types(list and tupe )
    print(x,end=' ')
#output
1 2 3 4 5 6 

chain.from_iterable accepts single iterable as argument

for x in chain.from_iterable(['ABC']):  # the iterable argument with nested 
iterable(**'ABC' is again iterable**) 
    print(x,end=' ')
#output 
A B C

Whereas this does not work

for x in chain.from_iterable([1,2,3]): 
    print(x,end=' ')
    #TypeError: 'int' object is not iterable 
Cribbage answered 26/9, 2021 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.