Python For Og Lecture 68 - Iterable Vs Iterator
Python For Og Lecture 68 - Iterable Vs Iterator
Website - https://petroleumfromscratchin.wordpress.com/
LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch
YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw
for i in por:
print(i)
0.14
0.2
/
2/3/2021 Python for O&G Lecture 68: Iterable vs Iterator - Colaboratory
0.42
0.26
for i in perm:
print(i)
40
24
29
37
25
for i in word:
print(i)
w
e
l
l
s
t
i
m
u
l
a
t
i
o
n
Iterable is an object, which one can iterate over. ... Iterator is an object, which is used to iterate over an iterable object using next() method
# 1.) for loop first converts it into an iterator object by calling function iter() -->> iter(por)
# 3.) stops iteration when next() can't find any further items
print(por)
type(por)
list
por_new = iter(por)
type(por_new)
list_iterator
next(por_new)
0.14
next(por_new)
for i in por:
print(i)
0.14
0.2
0.42
0.26
for i in por:
print(i)
/
2/3/2021 Python for O&G Lecture 68: Iterable vs Iterator - Colaboratory
0.14
0.2
0.42
0.26
Observation:
1.) Every iterator is also an iterable, but not every iterable is an iterator
# Now when we create a list or any iterable, at that time whole iterable is stored at a memory location which ends up taking huge memory
# in case of iterators, each elemnt is getting it's own memory only when we are calling next()
/
2/3/2021 Python for O&G Lecture 68: Iterable vs Iterator - Colaboratory