• Mastering Python Standard Library: infinite iterators of itertools

    Let’s continue our little research of itertools module.

    Today we’ll have a look at 3 infinite iterator constructors:

    from itertools import count, cycle, repeat

    Read more →

  • Mastering Python Standard Library: itertools.chain

    Imagine, you need to iterate over some N iterables.

    For example, you have two lists: l1 and l2.

    In [2]: l1 = list(range(5))
    In [3]: l2 = list(range(10))
    
    In [4]: l1
    Out[4]: [0, 1, 2, 3, 4]
    
    In [5]: l2
    Out[5]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    Here is the easiest way to do so:

    Read more →

  • Tricky Unpacking In Python

    Imagine, you iterate through a collection, which contains some other collections.

    Like so: list of lists

    In [32]: L = [ [i] for i in range(10) ]
    
    In [33]: L
    Out[33]: [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]

    One obvious way to iterate over inner values is to use indexing:

    Read more →