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!

Meeyaw
  • 75
  • 1
  • 8
  • 1
    Do you understand how generators work in python? If not, it's probably best to start there. – Patrick Haugh Apr 01 '17 at 00:03
  • Have you tried writing some code? It usually helps me to understand things I've read. Come up with a [mcve] to show what you don't understand. – Peter Wood Apr 01 '17 at 00:06
  • 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. – Meeyaw Apr 01 '17 at 00:07
  • See also: https://stackoverflow.com/q/15004772/610569 – alvas Jun 22 '20 at 15:15

3 Answers3

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.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
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.
pylang
  • 40,867
  • 14
  • 129
  • 121
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