> From: Tommy Grav
> 
> Hi everyone,
> 
>    I have a list of objects where I have want to do two loops.
> I want to loop over the list and inside this loop, work on all
> the elements of the list after the one being handled in the outer
> loop. I can of course do this with indexes:
> 
>  >>> alist = range(3)
>  >>> for i in xrange(len(alist)):
> ...   for j in xrange(i+1,len(alist)):
> ...     print i,j,alist[i],alist[j]
> ...
> 0 1 0 1
> 0 2 0 2
> 1 2 1 2
>  >>>
> 
> 
> Is there a way to do this without using indexes?
> 

You have to use indices because you are printing the indices.  Given
that, the following loop does what it looks like you are trying to do.

>>> alist = range(3)
>>> for index, i in enumerate(alist):
        for jndex, j in enumerate(alist[index:]):
                print index, jndex, i, j

                
0 0 0 0
0 1 0 1
0 2 0 2
1 0 1 1
1 1 1 2
2 0 2 2
>>>


--
-Bill Hamilton
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to