1

I try to program now in pythonic. I have next case - some array, which elements I want to iterate with each others... At the moment I coded next example:

a = ['a','b','c','d','e','f']

for posx in range(len(a)):
    for posy in range(posx+1, len(a)):
                *some operation for these elements*

Now I want to ask some experience Pyhton users, how I can reduce such slow for'loops? Is it possible to use here ziptool? How I could understand, zip just connect two elements on the same position in two difeerent lists (or arrays). I want to iterate one list over another and get back the operation for every element in both lists.

Thanks a lot

1 Answer 1

3

This is available in the standard library as itertools.combinations:

import itertools

for elem1, elem2 in itertools.combinations(a, 2):
    do_whatever_with(elem1, elem2)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, exactly answer. Accepted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.