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!
>>> 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
Post a Comment