Sobes.tech
Junior — Middle

How can a tuple be converted into an iterator in Python?

sobes.tech AI

Answer from AI

In Python, a tuple is already an iterable object, so to get an iterator, it is enough to call the iter() function on the tuple.

Example:

my_tuple = (1, 2, 3)
iterator = iter(my_tuple)

print(next(iterator))  # 1
print(next(iterator))  # 2
print(next(iterator))  # 3

Thus, converting a tuple to an iterator is simply calling iter().