Python itertools combinations in distance
Question: I'm using `itertools.combinations` to match all the possible combinations of a
list.

My list looks something like

    
    
    [[1,2],[2,3],[3,4],[4,5],[5,6]]
    

I understand how to get all the combinations, but if i want to use the
distance formula on each combination how would I go about doing this?

I don't know how to use each individual combination in this matter.


Answer: Simply loop over the output of the `combinations()` function:

    
    
    for xy1, xy2 in combinations(inputlist, 2):
        distance = calculate_distance(xy1, xy2)
    

This uses tuple unpacking; `combinations` produces tuples of two items each,
and Python assigns these to the two names. The first combination produced is
`([1, 2], [2, 3])`, but the loop assigns these to `xy1` (set to `[1, 2]` and
`xy2` (set to `[2, 3]`) respectively.

Quick demo just printing:

    
    
    >>> from itertools import combinations
    >>> inputlist = [[1,2],[2,3],[3,4],[4,5],[5,6]]
    >>> for xy1, xy2 in combinations(inputlist, 2):
    ...     print(xy1, xy2)
    ... 
    [1, 2] [2, 3]
    [1, 2] [3, 4]
    [1, 2] [4, 5]
    [1, 2] [5, 6]
    [2, 3] [3, 4]
    [2, 3] [4, 5]
    [2, 3] [5, 6]
    [3, 4] [4, 5]
    [3, 4] [5, 6]
    [4, 5] [5, 6]
    

