Hi, @Ramesh
You can follow the below-given lines to get a wider view with the difference:
- In creating a python generator, we use a function. But in creating an iterator in python, we use the iter() and next() functions.
- A generator in python makes use of the ‘yield’ keyword. A python iterator doesn’t.
- Python generator saves the states of the local variables every time ‘yield’ pauses the Loop in Python. An iterator does not make use of local variables, all it needs is iterable to iterate on.
- A generator may have any number of ‘yield’ statements.
- You can implement your own iterator using a python class: a generator does not need a class in python.
- To write a python generator, you can either use a python function or a comprehension. But for an iterator, you must use the iter() and next() functions.
- Generator in python lets us write fast and compact code. This is an advantage over Python iterators. They are also simpler to code than do custom iterator.
- Python iterator is more memory-efficient.
You can visit here https://www.edureka.co/community/29298/difference-between-iterators-generators#:~:text=Every%20generator%20is%20an%20iterator,paragraph's%20definition%20of%20an%20iterator. for more example details.