0% found this document useful (0 votes)
13 views

Python For Og Lecture 68 - Iterable Vs Iterator

The document discusses the difference between iterables and iterators in Python. Iterables are objects that can be iterated over like lists, tuples, strings and dictionaries. Iterators are objects that are used to iterate over iterables using the next() method. A for loop works by first converting the iterable to an iterator using iter(), then calling next() on the iterator object until there are no further items. Iterators are more memory efficient than iterables when dealing with large amounts of data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python For Og Lecture 68 - Iterable Vs Iterator

The document discusses the difference between iterables and iterators in Python. Iterables are objects that can be iterated over like lists, tuples, strings and dictionaries. Iterators are objects that are used to iterate over iterables using the next() method. A for loop works by first converting the iterable to an iterator using iter(), then calling next() on the iterator object until there are no further items. Iterators are more memory efficient than iterables when dealing with large amounts of data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2/3/2021 Python for O&G Lecture 68: Iterable vs Iterator - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

# iterables are string, tuples , lists and dictionaries

por = [0.14, 0.2, 0.42, 0.26] # por in fraction -list

perm = (40, 24, 29, 37, 25) # perm in md -- tuple

word = 'well stimulation'

# run for loop in all of them

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

# let us understand how for loop works

# 1.) for loop first converts it into an iterator object by calling function iter() -->> iter(por)

# 2.) next function ->> next()

# 3.) stops iteration when next() can't find any further items

# let us get same results of for loop by performing above stpes


/
2/3/2021 Python for O&G Lecture 68: Iterable vs Iterator - Colaboratory

print(por)

[0.14, 0.2, 0.42, 0.26]

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()

# so in cases where lot of data is required to be stored, iterator is a better choice

/
2/3/2021 Python for O&G Lecture 68: Iterable vs Iterator - Colaboratory

You might also like