Iterating over every two elements in a list in Python -


this question has answer here:

how iterate on pair combination in list, like:

list = [1,2,3,4] 

output:

1,2 1,3 1,4 2,3 2,4 3,4 

thanks!

use itertools.combinations:

>>> import itertools >>> lst = [1,2,3,4] >>> x in itertools.combinations(lst, 2): ...     print(x) ... (1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4) 

btw, don't use list variable name. shadows builtin function/type list.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -