Get startedGet started for free

What are iterable objects?

1. What are iterable objects?

Great job on the first chapter! Let's now dive into the world of iterable objects. What are they?

2. Definition

Simply put, iterable objects or Iterables represent any object that can be used in a for loop, meaning that it stores an element sequence. You already know a lot of Iterables: list, tuple, set, dictionary, and string.

3. Iterating through a list or tuple

Iterating through a list or tuple is very straightforward: the items are retrieved in the sequence we see them.

4. Iterating through a set

Set items, on the other hand, are retrieved disordered compared to how we inserted them.

5. Iterating through a string

Iterating through a string provides its character sequence.

6. Iterating through a dictionary

A dictionary is not that obvious. We get the keys but not the values.

7. Getting key-value pairs

To iterate through key-value pairs, we have to use dictionary .items() method.

8. Getting key-value pairs

We can unwrap each tuple right in the definition of the for loop. Here we use title and subtitle instead of item.

9. Less visual objects: range

So far, we mentioned pretty visual objects. But let's consider the range object we use a lot. It doesn't have a clear output representation like a list. It is Iterable though: it knows how to retrieve consecutive items when needed.

10. Less visual objects: enumerate

Another example is an enumerate object. To create it, we need to pass an Iterable to the enumerate() constructor. Looping over this object results in tuples which add an index to each item from the given Iterable. Since we deal with tuples, we can rewrite our loop

11. Less visual objects: enumerate

like this, to print the index and the corresponding element.

12. Iterables as arguments

Iterables can be passed to constructors such as list(), tuple(), set() and so on to create corresponding data structures. Taking, for example, the enumerate object from our previous example, we can easily convert it to a list

13. Iterables as arguments

or a set.

14. How to know if we deal with an Iterable

How to know if we deal with an Iterable? Here's the trick. We can apply the iter() function on it. If we deal with an Iterable, this function returns a special object called Iterator. This object knows how to retrieve consecutive elements from an Iterable one by one. It is because you can apply a special function on it called next() that returns the consecutive element in a given sequence.

15. StopIteration

We can call it until a StopIteration error is raised, indicating that there are no more values to iterate through.

16. Describing a for loop

Why is an Iterator important for an object to be Iterable? Let's see how a for loop works under the hood. Here's our Iterable. First, the associated Iterator is retrieved. Then, a while loop is created that stops only when StopIteration error is raised.

17. Describing a for loop

Inside the try block we apply the next() function on the Iterator and print the result. Here's the output of the code exactly matching the one with the for loop.

18. Many Iterables are Iterators

Many Iterables are actually Iterators meaning we can apply both functions on them. An enumerate and finditer object from the previous lesson are good examples.

19. iter() or next()

We can either loop over the object

20. iter() or next()

or apply the next() function.

21. Expendable Iterables

Iterables that are Iterators normally can be traversed only once. Looping over the same object again is not possible. That behavior contrasts with pure Iterables we can't apply the next() function on.

22. Traversing a DataFrame

A DataFrame also provides many possibilities for traversing its elements. Let's consider this DataFrame describing some characters from the Star Wars.

23. Direct approach

If we put the DataFrame directly in a for loop, each item will represent a column name. Let's consider other approaches.

24. .iterrows()

One is to loop over an object returned by the .iterrows() method.

25. .iterrows()

Each item in this case is a tuple consisting of a row index name and a data Series containing all the information on that row. Of course, it can be unwrapped

26. .iterrows()

like this.

27. .iteritems()

Another approach is to loop over an object returned by .iteritems() method.

28. .iteritems()

Each item in this case is a tuple containing a column name and a data Series with all the information in that column.

29. .iteritems()

It can be also unwrapped.

30. Let's practice!

We covered pretty much information on Iterables. Let's practice!