Felix Felix - 1 year ago 78
Python Question

python looping through 2 lists skipping similar elements

I have 2 identical lists
a = [a1,a2,a3]
b = [a1,a2,a3]
What is a most effective way to iterate over these 2 list simultaneously while i am interesting only in combination of different elements from both lists despite order, i.e a1a2 and a1a3. Combinations a1a1, a2a2, a3a3, a2a1, a3a1 i am interesting to skip, but interesting keep iterators values avaliable.

Want to re phrase questions:
interesting in possible combinations of 2 elements from list a = [a1,a2,a3]

Answer Source

Use combinations,

from itertools import combinations
for i in combinations(['a1','a2','a3'],2):
    print i
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download