Chain
- VC Healy

- Jul 26, 2020
- 1 min read
Adjoining iterables into one iterable object
The chain module is imported from the itertools library.
from itertools import chain
# Two lists (iterable objects)
fruit = ['apple ', 'banana ', 'strawberry ']
amount = ['1' ,'2' , '3']Combining them into a single list with the following
combo = list(chain(fruit, amount))
print(combo)combo will attach the lists in the order received in the command. The order of the content of each list will not be altered.
['apple ', 'banana ', 'strawberry ', '1', '2', '3']In the above example, a new list was created but replacing the keyword list with set or tuple will give the corresponding object type.




Comments