Python - Permutation/Combination column-wise
Question: I have a list

    
    
    mylist = [
        ['f', 'l', 'a', 'd', 'l', 'f', 'k'], 
        ['g', 'm', 'b', 'b', 'k', 'g', 'l'], 
        ['h', 'n', 'c', 'a', 'm', 'j', 'o'], 
        ['i', 'o', 'd', 'c', 'n', 'i', 'm'],
        ['j', 'p', 'e', 'e', 'o', 'h', 'n'], 
    ]
    

I want do permutation/combination column-wise, such the elements of the column
are restricted to that column i.e., f,g,h,i,j remain in Column 1, l,m,n,o,p
remain in Column 2 and so on, in the results of permutation/combination. How
can this be achieved in Python 2.7?


Answer: You could [use
`zip(*mylist)`](https://docs.python.org/2/library/functions.html#zip) to list
the "columns" of `mylist`. Then use [the `*`
operator](http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-
in-python/) (again) to unpack those lists as arguments to `IT.product` or
`IT.combinations`. For example,

    
    
    import itertools as IT
    list(IT.product(*zip(*mylist)))
    

yields

    
    
    [('f', 'l', 'a', 'd', 'l', 'f', 'k'),
     ('f', 'l', 'a', 'd', 'l', 'f', 'l'),
     ('f', 'l', 'a', 'd', 'l', 'f', 'o'),
     ('f', 'l', 'a', 'd', 'l', 'f', 'm'),
     ...]
    

