Google News
logo
Python Program to Return an iterator from a tuple
In the following example of Python program you can create an iterator from a tuple using the `iter()` function :
Program :
my_tuple = (1, 2, 3, 4, 5)
my_iterator = iter(my_tuple)

# Iterate through the iterator
for x in my_iterator:
  print(x)
Output :
1
2
3
4
5
In the above example, we first create a tuple `my_tuple` with 5 integers. We then create an iterator from the tuple using the `iter()` function and assign it to `my_iterator`. We can then loop through the iterator using a `for` loop and print out each value in the tuple.