Search code examples
pythonpython-itertools

datatype of the answer given while using itertools


Could anyone tell me what would be the data type of the answer given, while using itertools.combination(iterable, r)??

from itertools import combinations
def rSubset(arr, r):
 
    return list(combinations(arr, r))
if __name__ == "__main__":
    arr = [1, 2, 3, 4]
    r = 2
    print (rSubset(arr, r))

Solution

  • itertools.combinations returns a reference to an itertools.combinations class which is iterable. To clarify this using your data:

    from itertools import combinations
    
    arr = [1, 2, 3, 4]
    
    for comb in combinations(arr, 2):
      print(comb)
    

    Output:

    (1, 2)
    (1, 3)
    (1, 4)
    (2, 3)
    (2, 4)
    (3, 4)