Menu
Coddy logo textTech

Built-in Iterator Functions

Lesson 7 of 13 in Coddy's Python Iterators course.

Python provides several built-in functions that work with iterators, making it easier to manipulate and process iterable data. Let's examine three commonly used functions: enumerate(), zip(), and reversed().

1. enumerate()

The enumerate() function adds a counter to an iterable and returns it as an enumerate object. It's particularly useful when you need both the index and value of elements in an iterable.

fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")

# Output:
# 0: apple
# 1: banana
# 2: cherry

2. zip()

The zip() function creates an iterator of tuples where each tuple contains the i-th element from each of the input iterables. It's useful for combining multiple iterables.

names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for name, age in zip(names, ages):
    print(f"{name} is {age} years old")

# Output:
# Alice is 25 years old
# Bob is 30 years old
# Charlie is 35 years old

3. reversed()

The reversed() function returns a reverse iterator. It creates an iterator that accesses the given sequence in the reverse order.

numbers = [1, 2, 3, 4, 5]
for num in reversed(numbers):
    print(num)

# Output:
# 5
# 4
# 3
# 2
# 1

These built-in functions provide powerful tools for working with iterators, allowing you to easily add counters, combine iterables, or reverse sequences. They can significantly simplify your code and make it more readable when dealing with iterable data structures.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a program that processes a list of products and their prices, combining the enumerate(), zip(), and reversed() functions to generate a formatted output.

You are provided with the following inputs:

  • A string of product names, separated by commas
  • A string of corresponding prices, separated by commas

Your program should:

  1. Convert the input strings into lists of products and prices
  2. Use zip() to pair products with their prices
  3. Use reversed() to reverse the order of the paired items
  4. Use enumerate() to add a counter to each item
  5. Print each item in the format: "#{counter}. {product}: ${price}"

Print each formatted item on a new line.

Try it yourself

# Read input
products = input().split(',')
prices = input().split(',')

# Convert prices to floats
prices = [float(price) for price in prices]

# TODO: Write your code below
# Hint: Use zip(), reversed(), and enumerate() to process the data

# Print the formatted output
# Remember to print each item on a new line

All lessons in Python Iterators