Skip to main content

Python Iterables

· 6 min read
Femi Adigun
Founder & CEO of Horace

Every collection item in python is iterable

An iterable provides iterator for loop and comprehensions i.e list comprehensions, set comprehensions, dictionary comprehensions, and generator expressions. Don't fret if you don't understand these terms, we'll explain them in future lessons.

One of the standout attributes of Horace Learning we use close-to-real-scenarios for our examples.

Lets use a converstaion between a teacher and a student to explain iterables.

Teacher: "Today, we're going to talk about iterables and generators in Python. Can anyone give me an example of an iterable?"

Student: "Um, is a list an iterable?"

Teacher: "Excellent! Yes, a list is an iterable. Can you think of why we might use iterables?"

Student: "I think we use them in for loops, right?"

Teacher: "Correct! Iterables are very useful in for loops. Now, let's talk about generators. They're a special kind of iterable that generates values on-the-fly."

Student: "On-the-fly? What does that mean?"

Teacher: "It means the values are created as you need them, rather than all at once. This can be very memory-efficient for large datasets."

Student: "Oh, I see. Can you show us an example?"

Teacher: "Of course! Let's create a simple generator that yields the squares of numbers."

One easy way to demystify computer science is to think in your native language not the abstract machine language.

  • For our first exercise, we will:
    • Get all the words from the teacher's first sentence.
    • Count the number of words in the sentence.
    • Finally, find repeated words in the sentence.

Get the Teacher's First Sentence

sentence = "Today, we're going to talk about iterables and generators in Python."

Get all the words from the teacher's first sentence.

words = sentence.split()
print("All words:", words)

Count the number of words in the sentence.

word_count = len(words)
print("Word count:", word_count)

Find repeated words in the sentence.

word_frequency = {}
repeated_words = []

for word in words: # Convert to lowercase to ignore case
word = word.lower() # Remove punctuation
word = word.strip('.,')

# Count word frequency
if word in word_frequency:
word_frequency[word] += 1
if word_frequency[word] == 2:
repeated_words.append(word)
else:
word_frequency[word] = 1

print("Repeated words:", repeated_words)

NB: There are more efficient ways to find repeated words such asusing a set to store the words and then use a dictionary to count the frequency of each word, or using wordcloud with matplotlib. However, this lesson teaches iteration with the for keyword

Exercise 1:

  • Print the top 10 most frequent words in the conversation.

Solution

import string

# Combine all the dialogue into one string
conversation = """
Today, we're going to talk about iterables and generators in Python. Can anyone give me an example of an iterable?
Um, is a list an iterable?
Excellent! Yes, a list is an iterable. Can you think of why we might use iterables?
I think we use them in for loops, right?
Correct! Iterables are very useful in for loops. Now, let's talk about generators. They're a special kind of iterable that generates values on-the-fly.
On-the-fly? What does that mean?
It means the values are created as you need them, rather than all at once. This can be very memory-efficient for large datasets.
Oh, I see. Can you show us an example?
Of course! Let's create a simple generator that yields the squares of numbers.
"""

# Clean and split the text
words = conversation.lower().translate(str.maketrans('', '', string.punctuation)).split()

# Count word frequencies
word_freq = {}
for word in words:
if len(word) > 3: # Ignore short words
word_freq[word] = word_freq.get(word, 0) + 1

# Sort words by frequency
sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)

# Display top 10 words
print("Top 10 most frequent words:")
for word, freq in sorted_words[:10]:
print(f"{word}: {'*' * freq}")

How Does Iteration Work?

  • Python checks if the object is iterable by calling __iter__() method. i.e iter(object)
  • If the object is iterable, Python calls __iter__() method to get an iterator.
  • If it is not iterable, Python raises a TypeError exception:

    TypeError: 'int' object is not iterable

  • If __iter__() method is not implementd, but __getitem__() is, Python uses the __getitem__() method to iterate over the object by index starting from 0.
  • The iterator is an object with a __next__() method.
  • The __next__() method returns the next item in the sequence.
  • If the iterator is exhausted, Python raises a StopIteration exception.

Handson 2:

Understanding Iterables and Iterators in Python

1. Python Checks if the Object is Iterable

class SimpleIterable:
def __iter__(self):
return iter([1, 2, 3])

simple = SimpleIterable()
iterator = iter(simple) # This calls __iter__()
print(list(iterator)) # Output: [1, 2, 3]

If the object is not iterable, Python raises a TypeError exception:

try:
iter(42)
except TypeError as e:
print(e) # Output: 'int' object is not iterable

If __iter__() method is not implemented, but __getitem__() is, Python uses the __getitem__() method to iterate over the object by index starting from 0.

class SimpleIterable:
def __getitem__(self, index):
return [1, 2, 3][index]

simple = SimpleIterable()
iterator = iter(simple)
print(next(iterator)) # Output: 1

Iterator with __next__() method

class SimpleIterable:
def __iter__(self):
return iter([1, 2, 3])

simple = SimpleIterable()
iterator = iter(simple)
print(next(iterator)) # Output: 1

In summary, a pythonic object is iterable if it has __iter__() or __getitem__() method.

Clean code guide

  • If you will be iterating over an object, its not necessary to check if the object is iterable. Python will raise a TypeError exception if the object is not iterable. No point re-inventing the wheel. The built in iter() function will mostly be used by python itself than by the developer.
  • Use try except to catch the TypeError exception instead of doing explicit checks. we will discuss exceptions in future lessons

Further Exercises

  • Count all sentences by the teacher.
  • Count all sentences by the student.
  • Count all words by the teacher.
  • Count all words by the student.
  • Count all punctuation marks by the teacher.
  • Count all punctuation marks by the student.
  • Count all vowels by the teacher.
  • Count all vowels by the student.
  • Count all consonants by the teacher.
  • Count all consonants by the student.
  • Count all numbers by the teacher.
  • Count all numbers by the student.
  • Count all special characters by the teacher.
  • Count all special characters by the student.