Managing a computation onlt with iterators Python
Question: I'm trying to do this very simple thing in a more pythonistic way which would
involve only one iterator:

    
    
    >>>for i in xrange(10):
    ...    for j in xrange(i+1,10):
    ...        print i,j
    0 1
    0 2
    0 3
    0 4
    0 5
    0 6
    0 7
    0 8
    0 9
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    2 3
    2 4
    2 5
    2 6
    2 7
    2 8
    2 9
    3 4
    3 5
    3 6
    3 7
    3 8
    3 9
    4 5
    4 6
    4 7
    4 8
    4 9
    5 6
    5 7
    5 8
    5 9
    6 7
    6 8
    6 9
    7 8
    7 9
    8 9
    

What I'd like to do is something like this, which would involve only one
iterator:

    
    
    >>>from itertools import tee
    >>>iter1=iter(xrange(10))
    >>>for i in iter1:
    ...    iter2=tee(iter1,1)[0]
    ...    for j in iter2:
    ...        print i,j
    

This obviously doesn't work, yielding:

    
    
    0 1
    0 2
    0 3
    0 4
    0 5
    0 6
    0 7
    0 8 
    0 9
    

Since when I've iterated all the way through iter2 I've also iterated all the
way through iter1.

Is there any way I can manage to achieve this ? I'm using Python-2.7

**EDIT** : What I'd like to do is the same thing with any iterable, not only
numbers.


Answer: Use
[itertools.combinations](http://docs.python.org/2/library/itertools.html#itertools.combinations):

    
    
    import itertools
    for i, j in itertools.combinations(range(10), 2):
        print i, j
    

**EDIT** correnspond to EDIT of the question

itertools.combinations accept iterable. For example,

    
    
    >>> import itertools
    >>> 
    >>> def gen():
    ...     yield 'egg'
    ...     yield 'spam'
    ...     yield 'ham'
    ... 
    >>> for i, j in itertools.combinations(gen(), 2):
    ...     print i, j
    ... 
    egg spam
    egg ham
    spam ham
    

