Skip to content

Instantly share code, notes, and snippets.

@PythonDotLand
Created June 29, 2021 07:38
Show Gist options
  • Select an option

  • Save PythonDotLand/d495f713798cfb3c37950b57d562a912 to your computer and use it in GitHub Desktop.

Select an option

Save PythonDotLand/d495f713798cfb3c37950b57d562a912 to your computer and use it in GitHub Desktop.
>>> # Calculate all profucs of an input
>>> list(itertools.product('abc', repeat=2))
[('a', 'a'), ('a', 'b'), ('a', 'c'),
('b', 'a'), ('b', 'b'), ('b', 'c'),
('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> # Calculate all permutations
>>> list(itertools.permutations('abc'))
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'),
('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
>>> # Take elements for iterator as long as predicate is True
>>> list(itertools.takewhile(lambda x: x<5, [1,4,6,4,1]))
[1, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment