python iter
时间: 2024-01-16 08:05:35 浏览: 20
`iter()` is a built-in Python function used to create an iterator object. An iterator is an object that produces a sequence of values, one at a time, when iterated over. The `iter()` function takes an iterable object (such as a list, tuple, or string) as an argument and returns an iterator object.
Example:
```
my_list = [1, 2, 3, 4, 5]
# create an iterator object using iter()
my_iterator = iter(my_list)
# iterate over the iterator object using next()
print(next(my_iterator)) # output: 1
print(next(my_iterator)) # output: 2
print(next(my_iterator)) # output: 3
print(next(my_iterator)) # output: 4
print(next(my_iterator)) # output: 5
# when there are no more values to iterate over, next() raises StopIteration
print(next(my_iterator)) # raises StopIteration
```