Itertools

Count

  1. from itertools import count
  2. # infinite counting
  3. for i in count(10, 2):
  4. print(i)
  5. if i > 20:
  6. break
  7. #ans: 10, 12, 14, 16, 18, 20

Cycle

  1. from itertools import cycle
  2. # infinite cycling
  3. colors = ['red', 'green', 'blue']
  4. color_cycle = cycle(colors)
  5. for i, color in enumerate(color_cycle):
  6. print(color)
  7. if i >= 5:
  8. break
  9. #ans: red, green, blue, red, green, blue

Repeat

  1. from itertools import repeat
  2. # repeat value
  3. list(repeat(10, 3))
  4. #ans: [10, 10, 10]

Chain

  1. from itertools import chain
  2. # combine iterables
  3. list(chain([1, 2], [3, 4], [5, 6]))
  4. #ans: [1, 2, 3, 4, 5, 6]

Islice

  1. from itertools import islice
  2. # slice iterator
  3. list(islice(range(10), 2, 8, 2))
  4. #ans: [2, 4, 6]
  5. #ans: take first n
  6. list(islice(count(), 5))
  7. #ans: [0, 1, 2, 3, 4]

Product

  1. from itertools import product
  2. # cartesian product
  3. list(product([1, 2], ['a', 'b']))
  4. #ans: [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

Permutations

  1. from itertools import permutations
  2. # all orderings
  3. list(permutations([1, 2, 3], 2))
  4. #ans: [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

Combinations

  1. from itertools import combinations
  2. # unique selections
  3. list(combinations([1, 2, 3, 4], 2))
  4. #ans: [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

Exercises - Part 1

  1. from itertools import count
  2. # count is infinite?
  3. c = count()
  4. next(c)
  5. #ans: 0
  6. next(c)
  7. #ans: 1
  8. #ans: never stops

Exercises - Part 2

  1. from itertools import cycle
  2. # cycle repeats?
  3. c = cycle([1, 2])
  4. [next(c) for _ in range(5)]
  5. #ans: [1, 2, 1, 2, 1]

Exercises - Part 3

  1. from itertools import repeat
  2. # repeat without count?
  3. r = repeat(5)
  4. #ans: infinite
  5. list(islice(r, 3))
  6. #ans: [5, 5, 5]

Exercises - Part 4

  1. from itertools import chain
  2. # chain vs +?
  3. chain([1], [2], [3])
  4. #ans: iterator
  5. [1] + [2] + [3]
  6. #ans: [1, 2, 3] (list)

Exercises - Part 5

  1. from itertools import islice
  2. # islice with step?
  3. list(islice(range(20), 0, 10, 2))
  4. #ans: [0, 2, 4, 6, 8]

Exercises - Part 6

  1. from itertools import product
  2. # product with repeat?
  3. list(product([0, 1], repeat=3))
  4. #ans: [(0,0,0), (0,0,1), (0,1,0), (0,1,1),
  5. #ans: (1,0,0), (1,0,1), (1,1,0), (1,1,1)]

Exercises - Part 7

  1. from itertools import permutations
  2. # permutations vs combinations?
  3. list(permutations([1, 2], 2))
  4. #ans: [(1, 2), (2, 1)]
  5. list(combinations([1, 2], 2))
  6. #ans: [(1, 2)]

Exercises - Part 8

  1. from itertools import combinations
  2. # combinations order?
  3. list(combinations([3, 1, 2], 2))
  4. #ans: [(3, 1), (3, 2), (1, 2)]
  5. #ans: maintains input order

Exercises - Part 9

  1. from itertools import groupby
  2. # group consecutive
  3. data = [1, 1, 2, 2, 2, 3]
  4. [(k, list(g)) for k, g in groupby(data)]
  5. #ans: [(1, [1, 1]), (2, [2, 2, 2]), (3, [3])]

Exercises - Part 10

  1. from itertools import zip_longest
  2. # zip with fillvalue
  3. list(zip_longest([1, 2], ['a', 'b', 'c'], fillvalue=0))
  4. #ans: [(1, 'a'), (2, 'b'), (0, 'c')]

Google tag (gtag.js)