Nested for loop in python
Question: I'm trying to learn the python scripting . Being some body who started coding
in c/c++ or java i'm finding it extremely hard to find ways to write loops in
python , especially **for** loops with conditions

I've a list A of string, i need to do a specific op on these strings a pair at
a time ,say xor of the strings Also xor(a,b)=xor(b,a), hence i need to remove
the redundant pair while looping

In traditional lang i would do something like

    
    
    for(i=0;i<len;i++){
        for(j=i+1;j<len;j++){
            res[count]=xor(a[i],a[j])
            count++;
        }
    }
    

So how do I implement the same with Python, I could think of iterators but is
there a more efficient way , something very obvious eluding my eyes???


Answer: Python comes with [batteries
included](http://docs.python.org/tutorial/stdlib.html#batteries-included),
that is, most of the stuff like this is already written for you. If you want
combinations of strings, there is a [dedicated
function](http://docs.python.org/library/itertools.html#itertools.combinations)
for that:

    
    
    import itertools
    
    result = []
    for pair in itertools.combinations(a, 2):
        result.append(xor(pair[0], pair[1]))
    

or simply:

    
    
     result = [xor(*p) for p in itertools.combinations(a, 2)]
    

