Iterators vs. Generators in Python : Data Science Code

  Рет қаралды 19,184

ritvikmath

ritvikmath

Күн бұрын

Пікірлер: 38
@steevensonemile
@steevensonemile Жыл бұрын
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.
@abhishekdubey9920
@abhishekdubey9920 8 ай бұрын
Absolutely correct
@pranav0207
@pranav0207 2 ай бұрын
its mentioned in the video, sherlock
@sungchulyonseiackr
@sungchulyonseiackr 4 жыл бұрын
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.
@ashutoshsharmaxi
@ashutoshsharmaxi 2 жыл бұрын
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
@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 ?
@garrettwill1220
@garrettwill1220 3 ай бұрын
​@@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_controls
@money_wins_controls 3 ай бұрын
@@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.
@garrettwill1220
@garrettwill1220 3 ай бұрын
​@@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
@garrettwill1220
@garrettwill1220 3 ай бұрын
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
@antonboiko788
@antonboiko788 4 жыл бұрын
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_amara
@ramarao_amara 3 жыл бұрын
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.s3819
@shreyasb.s3819 4 жыл бұрын
Nice explained
@ritvikmath
@ritvikmath 4 жыл бұрын
Thank you 😃
@chap_eau
@chap_eau 6 ай бұрын
great video, useful and clear, thank you!
@classgames5825
@classgames5825 7 ай бұрын
Good explanation
@mykola_rudenko
@mykola_rudenko 2 жыл бұрын
Thank you for explanation!
@ashishk81
@ashishk81 4 жыл бұрын
Is range have more space complexity than xrange And xrange has more time complexity than range ?
@ritvikmath
@ritvikmath 4 жыл бұрын
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
@ashishk81
@ashishk81 4 жыл бұрын
@@ritvikmath Great explanation , as always 🙌
@kaushit
@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.deaquino4731
@fabiof.deaquino4731 4 жыл бұрын
Awesome!
@Jorge-hn1ye
@Jorge-hn1ye 4 жыл бұрын
Great video!
@CardiganBear
@CardiganBear Жыл бұрын
For an integer n, loading each value into memory is ridiculous.
@Irades
@Irades Жыл бұрын
it was clear, thanks!
@ritvikmath
@ritvikmath Жыл бұрын
Glad it helped!
@1ststandardcbse908
@1ststandardcbse908 2 жыл бұрын
Thank you so much
@MusicDABIsMe
@MusicDABIsMe 3 жыл бұрын
Great video, thank!
@ritvikmath
@ritvikmath 3 жыл бұрын
You are welcome!
@sdaiwepm
@sdaiwepm Жыл бұрын
Iterators and generators are not distinct - generators are a subset of iterators.
@pulkitaneja1739
@pulkitaneja1739 3 ай бұрын
I never drop comments on youtube videos, but @ritvikmath YOU ARE AWESOME. Beautiful explanation :)
@cocoarecords
@cocoarecords 4 жыл бұрын
If i only found thiS before my exam
@sandyjust
@sandyjust 4 жыл бұрын
Python 2 xrange to Python 3 range. I think in python3, now both are same.
@ryanlong1795
@ryanlong1795 4 жыл бұрын
cool!!!!!!!!!!!!!!!!!!Thanksssssssssssssssssssssssssssssssssss
@sgamer1770
@sgamer1770 4 жыл бұрын
maybe also touch on iter() and next() vs a generator object =(for *num for num in range(10) ) for example
@ritvikmath
@ritvikmath 4 жыл бұрын
good idea!
@jay_wright_thats_right
@jay_wright_thats_right 11 ай бұрын
I'm not feeling the neck beard but the chest hair is super sexy.
Pandas Indexing in Python : Data Science Code
14:29
ritvikmath
Рет қаралды 1,8 М.
Haunted House 😰😨 LeoNata family #shorts
00:37
LeoNata Family
Рет қаралды 15 МЛН
PRANK😂 rate Mark’s kick 1-10 🤕
00:14
Diana Belitskay
Рет қаралды 11 МЛН
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 138 МЛН
Человек паук уже не тот
00:32
Miracle
Рет қаралды 4,3 МЛН
Vectorization in Python : Data Science Code
11:12
ritvikmath
Рет қаралды 42 М.
Python Generators
15:32
mCoding
Рет қаралды 141 М.
Iterable VS Iterator Explained In Python
8:23
Indently
Рет қаралды 14 М.
Python Generators Explained
28:37
Tech With Tim
Рет қаралды 159 М.
Class Iterators : Python Basics
11:34
ritvikmath
Рет қаралды 4,8 М.
Advanced Python Series - Iterators Vs Generators
16:18
Krish Naik
Рет қаралды 54 М.
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 451 М.
Python Coding Problem: Creating Your Own Iterators
13:47
Corey Schafer
Рет қаралды 67 М.
R vs Python
7:07
IBM Technology
Рет қаралды 336 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 286 М.
Haunted House 😰😨 LeoNata family #shorts
00:37
LeoNata Family
Рет қаралды 15 МЛН