Skip to content

Instantly share code, notes, and snippets.

@AaronPhalen
Last active April 21, 2020 03:26
Show Gist options
  • Save AaronPhalen/0c64e25697f34d752a89 to your computer and use it in GitHub Desktop.
Save AaronPhalen/0c64e25697f34d752a89 to your computer and use it in GitHub Desktop.
Useful modules and examples from Python's itertools library.
# python_itertools.py highlights useful functions in python's itertools module
# Influenced by Raymond Chandler III pyvideo.org/video/2275 (Super Advanced Python Concepts)
# Author: Aaron Phalen | Twitter: @aaron_phalen | Email: [email protected]
from itertools import chain, combinations, permutations, compress, count, cycle, groupby
# 1. Itertools Chain Example
letters = ["a", "b", "c", "d"]
numbers = [1, 2, 3]
both = chain(letters, numbers)
# Two methods for iterating through itertools chain generator-like expression.
# Useful for reducing memory footprint when working with large data sets.
# Method 1: for loop
for b in both:
print b,
# Output: a, b, c, d, 1, 2, 3
# Method 2: while loop
while True:
try:
elem = both.next()
print elem,
except StopIteration:
break
# Output: a, b, c, d, 1, 2, 3
# Note: must re-populate both if trying to run both method 1 and 2 above, due to generator-like behavior.
# 2. Itertools Combinations Example
letters = ["a", "b", "c"]
print list(combinations(letters, 2))
# Output: [('a', 'b'), ('a', 'c'), ('b', 'c')]
# Note: Itertools combinations returns all input list combinations of n-element tuple.
# As given as an example above, letters as input list and 2 as n-element tuple.
# Combinations does not care about result order and will return unique combinations.
# 3. Itertools Permutations Example
numbers = [1, 2, 3]
print list(permutations(num_list, 2))
# Output: [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
# Note: Itertools permutations cares about order and will return all permutations.
# 4. Itertools Compress Example
candidates = ["Obama", "Trump", "Clinton" "Mcafee"]
win_list = [1, 1, 0, 0]
print list(compress(candidates, win_list))
#Ouput: ["Obama", "Trump"]
# Note: Itertools compress is very similar in function to filter in Python.
# Obama, Trump in output because of incumbent and Trump's likeliness to win.
# 5. Itertools Count Example
c = count(1, 5) # itertools count generator-like object returns 1 to infinity by 5
for _ in xrange(0, 3):
print c.next(),
# Output: 1, 6, 11
# 6. Itertools Cycle Example
colors = cycle(["red", "green", "blue", "yellow"])
for _ in xrange(0, 6):
print colors.next(),
# Output: red, green, blue, yellow, red
# Note: Cycle will infinitely iterate though an iterable in an orderly fashion and
# return to start index to repeat.
# 7. Itertools Group By
animals = [("dog", "dachshund"), ("dog", "poodle"), ("insect", "ant")]
for key, group in groupby(animals, key=lambda x: x[0]):
print key
for g in group:
print g[0]
# Output: dog
# dachshund
# poodle
#
# insect
# ant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment