What is difference between genarator and iterator

0 votes
Aug 17, 2020 in Python by Ramesh
• 120 points
109 views

2 answers to this question.

0 votes

Hi, @Ramesh

You can follow the below-given lines to get a wider view with the difference:

  1. In creating a python generator, we use a function. But in creating an iterator in python, we use the iter() and next() functions.
  2. A generator in python makes use of the ‘yield’ keyword. A python iterator doesn’t.
  3. 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.
  4. A generator may have any number of ‘yield’ statements.
  5. You can implement your own iterator using a python class: a generator does not need a class in python.
  6. 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.
  7. 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.
  8. 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.

answered Aug 17, 2020 by Raina Neman
0 votes

Hello @ Ramesh

Generator Iterator
  • Implemented using a function.
  • Implemented using a class.
  • Uses the yield keyword.
  • Does not use the yield keyword.
  • Usage results in a concise code.
  • Usage results in a relatively less concise code.
  • All the local variables before the yield statements are stored.
Hope it helps!!
Thank You!!
  • No local variables are used.
answered Aug 17, 2020 by Niroj
• 82,720 points

Related Questions In Python

0 votes
1 answer

What is the difference between list and tuple?

Lists are mutable(values can be changed) whereas ...READ MORE

answered May 4, 2018 in Python by aayushi
• 750 points
3,606 views
+1 vote
2 answers

What is the difference between classes and labels in machine learning?

Classes and Labels both are almost same things ...READ MORE

answered Apr 3, 2019 in Python by SA
• 1,050 points
1,476 views
0 votes
1 answer

What is the difference between Python and IPython?

There are few differences between Python and ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 58,120 points
2,364 views
0 votes
1 answer

What is the difference between re.search and re.match?

The theoritical approach can be this way, re.match is ...READ MORE

answered Aug 10, 2018 in Python by Priyaj
• 58,120 points
4,602 views
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 58,120 points
1,101 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,032 views
0 votes
0 answers
0 votes
1 answer

For Professional window GUI develepoment which python lib/module/framework is best?

HI, @Ganesh, Tkinter is one of the most ...READ MORE

answered Dec 24, 2020 in Python by Gitika
• 65,870 points
72 views