Lecture 12 Iterators and Generators
- Questions
- why is map considered easily
- how does the countdown function keep track of k
- Does yield skip to the next one?
- Iterables
- objects (or data) that can be iterated over
- contains some elements in some order that can be kept track of by going from one element to the next; looped over
- Can i use it in a for loop or list comprehension?
- if yes, then it is an iterable
- Examples
- list
- string
- dictionary
- range
- tuple
- Iterators
- Iterators
- Container can provide an iterator that provides access to its elements in order
- iter(iterable)
- return an iterator over the elements of an iterable value
- next(iterator)
- return the next element in an iterator
- If we start a new iterator, it starts from the beginning
- We error if we use iterable when there are no more elements
- Can pass iterator into a list
- interpreter returns the rest of the iterable’s values
- Arrow pointer is stored as an index
- if you get rid of elements, then the new index value is evaluated
- Iterators are like bookmarks
- An iterator is an iterable
- Dictionary Iteration
- View of a Dictionary
- iterator returned from iter and can be passed to next;
- all iterators are mutable
- A dictionary, its keys, its values, and its items are all iterable values
- The order of items in a dictionary is the order in which they were added (Python 3.6+)
- For Statements
- Can put iterators in a for loop
- will complete the iterator in a normal for loop
- Built-in Iterator Functions
- Many results are computed lazily
- Only computed when needed
- Examples
- map(func, iterable)
- iterate over func(x) for x in iterable
- filter(func, iterable)
- iterate over x in iterable if func(x)
- zip(first_iter, second_iter):
- reversed(sequence)
- iterate over x in a sequence in reverse order
- To view the contents of an iterator, place the result elements into a container
- list(iterable)
- tuple(iterable)
- sorted(iterable)
- Map returns an iterator
- only computers when calling next on it
- Reversed doesnt auto compute reverse of t, only when you ask it to
- The Zip Function
- Returns an iterator over co-indexed tuples
- Zip skips extra numbers
- Using Iterators
- Reasons for Using Iterators
- Code that processes an iterator (via next) or iterable (via for or iter) makes few assumptions about the data itself
- useful for ensuring that each element of a sequence is processed only once
- limits the operations that can be performed on a sequence by only calling next
- Generator
- special type of iterator
- Generators and Generator Functions
- can yield multiple values
- yield is like a return statement, but you can yield multiple values
- A generator function can yield multiple times
- generator is an iterator created automatically by calling a generator function
- generator functions when called, returns a generator that iterates over its yields
- All generators are iterators
- iterates through its yield statements
- Generator Functions can Yield from Iterables
- yield from statement yields all values from an iterator or iterable
- basically a for loop that yields all the values from a generator
- You can yield all values from recursive call with yield from
- Yielding Partitions
- 3