If you're watching this video in 2023, please note that xrange has been removed from Python 3, and the range function now behaves like xrange did in Python 2.
@abhishekdubey99208 ай бұрын
Absolutely correct
@pranav02072 ай бұрын
its mentioned in the video, sherlock
@sungchulyonseiackr4 жыл бұрын
I think you have to compare memory usage, not time to see the real advantage of generator against iterator. When I use a generator, my major concern is memory, not time. Suppose you have 100GB data to handle. I saw such big data from astronomy. You cannot load that big one on memory. However, this can be handled by a generator.
@ashutoshsharmaxi2 жыл бұрын
hey! you asked a briliant question, my answer to this is, range() might be faster and storage efficient as it is just creating an instance of the range in a variable and remembering the last known number that we provide whether range() creates the whole list of numbers up to the given range.
@money_wins_controls Жыл бұрын
do generators work only in a sequence of numbers like from 1 to 100000 like range(1,100000) since it loads just that current number in memory or a sequence of numbers like range(1,100000,2) with a step count ?
@garrettwill12203 ай бұрын
@@money_wins_controls Ik this comment is old, but generators work not only for numbers but for anything. can return strings other generators, objects etc. range specifically is a generator of numbers and it can indeed handle a step count, a start and a stop point.
@money_wins_controls3 ай бұрын
@@garrettwill1220 but my doubt is how it works with strings. with numbers range it can keep track by adding +1. with strings i just want to know how it works.
@garrettwill12203 ай бұрын
@@money_wins_controls theres not a built in generator for strings that i am aware of but you simply build your own as range is simply a generator that looks like this: def my_range(stop): # The `start` is implicitly 0 and `step` is implicitly 1. start = 0 step = 1 # Use a while loop to generate values current = start while current < stop: yield current current += step this is simplified because only the stop point is modifiable but you get the point. if you want to return things other than ints or numbers not in a sequence you can use a generator function to do the same thing. def generator(): for i in range(10): yield 'G' * (i+1) gen = generator() for thing in gen: print(thing) The output would look like: G GG GGG GGGG GGGGG GGGGGG GGGGGGG GGGGGGGG GGGGGGGGG GGGGGGGGGG Or this alphabet generator would gen the next letter in the alphabet each time its called. def alphabet_generator(): # Start from 'a' current_char = 'a' while current_char
@garrettwill12203 ай бұрын
I personally would redo the test at the end, as I think it was misleading. I tested it on my laptop and my PC and experience no speed difference in small numbers for a single iteration. On my pc the difference between a list comprehension [x for x in range(100000)] vs a generator only makes a difference when my memory exceeds a certain threshold. And i would show an example like with open (file.txt) as file for row in file
@antonboiko7884 жыл бұрын
Hey! Thank you for the video, the main idea behind usage of generators and iterators was clearly explained. I'm wondering is there any explanation why we have the picks on the graph? Just a little doubtful concerning the time evaluation method.
@ramarao_amara3 жыл бұрын
In python3 , seems range is not a list , it will returns range object, like xrange in python2 and xrange in python2 & range in python3 are Iterables not Iterators/Generators import collections issubclass(range, collections.Iterator) -> False issubclass(range, collections.Generator) -> False issubclass(range, collections.Iterable) -> True same as with xrange. and its not loading entire data into memory import sys sys.getsizeof(range(100)) -> 48 sys.getsizeof(list(range(100))) -> 1008 issubclass(collections.Iterator, collections.Iterable) ->True issubclass(collections.Generator, collections.Iterable) ->True issubclass(collections.Generator, collections.Iterator) ->True ** python iterator is much more memory efficient than generator **
@shreyasb.s38194 жыл бұрын
Nice explained
@ritvikmath4 жыл бұрын
Thank you 😃
@chap_eau6 ай бұрын
great video, useful and clear, thank you!
@classgames58257 ай бұрын
Good explanation
@mykola_rudenko2 жыл бұрын
Thank you for explanation!
@ashishk814 жыл бұрын
Is range have more space complexity than xrange And xrange has more time complexity than range ?
@ritvikmath4 жыл бұрын
That is a good way to think about it. range will import all numbers at once so it has a high space complexity xrange will give you numbers as you need them so has a lower space complexity but those function calls to the xrange generator are typically more expensive than reading a number already in memory
@ashishk814 жыл бұрын
@@ritvikmath Great explanation , as always 🙌
@kaushit Жыл бұрын
it's wrong generator and range function both are similarly in terms of memory usage that means xrange and range both generate elements on fly without pre loading whole range in memory first, just double checked things by coding itself.
@fabiof.deaquino47314 жыл бұрын
Awesome!
@Jorge-hn1ye4 жыл бұрын
Great video!
@CardiganBear Жыл бұрын
For an integer n, loading each value into memory is ridiculous.
@Irades Жыл бұрын
it was clear, thanks!
@ritvikmath Жыл бұрын
Glad it helped!
@1ststandardcbse9082 жыл бұрын
Thank you so much
@MusicDABIsMe3 жыл бұрын
Great video, thank!
@ritvikmath3 жыл бұрын
You are welcome!
@sdaiwepm Жыл бұрын
Iterators and generators are not distinct - generators are a subset of iterators.
@pulkitaneja17393 ай бұрын
I never drop comments on youtube videos, but @ritvikmath YOU ARE AWESOME. Beautiful explanation :)
@cocoarecords4 жыл бұрын
If i only found thiS before my exam
@sandyjust4 жыл бұрын
Python 2 xrange to Python 3 range. I think in python3, now both are same.