Python OOPs


Python SQLite


Examples


Others


Python iter() Function


The iter() method in Python creates an iterator from an iterable, allowing us to traverse a collection one element at a time. Here's a simple example to understand how it works.

syntax
iter(object, sentinel)
  • Object - Required. An iterable Object
  • sentinel - Optional. The sentinel value is a special value that stores the end of the sequence.
Example
# Define a list
my_list = [1, 2, 3, 4]

# Create an iterator from the list
my_iterator = iter(my_list)

# Use the iterator to traverse through the list
print(next(my_iterator)) 
print(next(my_iterator))  
print(next(my_iterator))  
print(next(my_iterator))  

Output

1
2
3
4