2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Itertools
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Itertools is a python module of functions that return generators, objects that only function when iterated over. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Some examples of itertool functions include but not limited to: chain(), imap(), product(), and compress().
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Iterators terminating on the shortest input sequence:
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### chain()
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `chain()`  function takes several iterators as arguments and returns a single iterator that produces the contents of all of them as though they came from one sequence.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```py
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import itertools
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								list(itertools.chain([1, 2], [3, 4]))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# Output
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# [1, 2, 3, 4]
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### islice()
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `islice()`  function returns an iterator which returns selected items from the input iterator, by index. It takes the same arguments as the slice operator for lists: start, stop, and step. Start and stop are optional.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```py
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import itertools
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								list(itertools.islice(count(), 5))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# Output
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# [0,1, 2, 3, 4]
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### izip()
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`izip()`  returns an iterator that combines the elements of several iterators into tuples. It works like the built-in function `zip()` , except that it returns an iterator instead of a list. 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```py
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import itertools
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								list(izip([1, 2, 3], ['a', 'b', 'c']))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# Output
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# [(1, 'a'),(2, 'b'),(3, 'c')]
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Combinatoric iterators
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### product()
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`product()`  returns a Cartesian product, equivalent to a nested for-loop. In comparison, the usual `zip()`  function, which returns the convolution. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```py
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from itertools import product
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								list(product([1,2,3],[3,4]))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# Output
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A = [[1,2,3],[3,4,5]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								list(product(*A))
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Output
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# [(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								B = [[1,2,3],[3,4,5],[7,8]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								list(product(*B))
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Output
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# [(1, 3, 7), (1, 3, 8), (1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 3, 7), (2, 3, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (3, 3, 7), (3, 3, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8)]
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 19:26:29 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Source: https://docs.python.org/3/library/itertools.html
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								https://www.hackerrank.com/challenges/itertools-product/problem