Iterable Uses
We know that lists are one type of built-in iterable objects. You may have also encountered the range(start, end)
function, which creates an iterable of ascending integers from start (inclusive) to end (exclusive).
>>> for x in range(1, 6):
... print(x)
...
1
2
3
4
Ranges are useful for many things, including performing some operations for a particular number of iterations or iterating through the indices of a list.
There are also some built-in functions that take in iterables and return useful results:
map(f, iterable)
- Creates iterator overf(x)
for eachx
initerable
filter(f, iterable)
- Creates iterator overx
for eachx
initerable
iff(x)
zip(iter0, iter2)
- Creates iterator over co-indexed pairs(x, y)
from both input iterablesreversed(iterable)
- Creates iterator over all the elements in the input iterable in reverse orderlist(iterable)
- Creates a list containing all the elements in the input iterabletuple(iterable)
- Creates a tuple containing all the elements in the input iterablesorted(iterable)
- Creates a sorted list containing all the elements in the input iterable