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

12.1 Python's Built-In Iterables and Iterators PDF

Python provides functions that return iterables and iterators. Iterators perform lazy evaluation and can only be iterated over once, while iterables can be iterated over many times. It is important to know whether an object is an iterable or iterator because iterables allow multiple iterations but iterators only allow single iteration. Examples of iterators include those returned by range, zip, enumerate and opening a file, while dictionary key, value, and item methods return iterables.

Uploaded by

Dragan Petrovic
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)
66 views

12.1 Python's Built-In Iterables and Iterators PDF

Python provides functions that return iterables and iterators. Iterators perform lazy evaluation and can only be iterated over once, while iterables can be iterated over many times. It is important to know whether an object is an iterable or iterator because iterables allow multiple iterations but iterators only allow single iteration. Examples of iterators include those returned by range, zip, enumerate and opening a file, while dictionary key, value, and item methods return iterables.

Uploaded by

Dragan Petrovic
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/ 4

Python provides many functions that return iterables or iterators

Additionally, the iterators perform lazy evaluation

You should always be aware of whether you are dealing with an iterable or an iterator

why? if an object is an iterable (but not an iterator) you can iterate over it many times

if an object is an iterator you can iterate over it only once


range(10) → iterable

zip(l1, l2) → iterator

enumerate(l1) → iterator

open('cars.csv') → iterator

dictionary .keys() → iterable

dictionary .values() → iterable

dictionary .items() → iterable

and many more…


Code
Exercises

You might also like