Skip to content

Advanced Collections and Built-in Functions

By the end of this lesson you'll be able to:

  • Use enumerate() for indexed loops and zip() to combine iterables
  • Write flexible functions with *args and **kwargs
  • Build dictionaries from two lists using zip()
  • Use max(), min(), sum(), sorted(), reversed(), map(), and filter() with confidence

Python's built-in functions are a powerful toolkit. This chapter gathers the most useful ones so you can write shorter, cleaner code that does more.

Welcome to Chapter 22!

Monty waving welcome Python's built-in toolkit is like a Swiss Army knife — there's a tool for almost everything! Today we're learning the ones you'll reach for most often. Let's code it together!

enumerate() — Loop with an Index

enumerate(iterable) wraps a list (or any sequence) and gives you both the index and the item on each loop iteration. Without enumerate(), you'd need a separate counter variable.

1
2
3
4
5
6
7
8
# Without enumerate — manual counter:
fruits = ["apple", "banana", "mango"]
for i in range(len(fruits)):
    print(i, fruits[i])

# With enumerate — cleaner:
for i, fruit in enumerate(fruits):
    print(i, fruit)

Both produce the same output. enumerate() starts at 0 by default; use enumerate(fruits, 1) to start at 1.


  

zip() — Combine Iterables in Parallel

zip(a, b) pairs up items from two (or more) iterables step by step. It stops when the shortest iterable runs out.

1
2
3
4
5
names  = ["Alice", "Bob", "Carol"]
scores = [95, 87, 91]

for name, score in zip(names, scores):
    print(f"{name}: {score}")

What Do You Think Will Happen?

Monty thinking The code below zips two lists of different lengths. How many pairs do you think zip() will produce? Make your guess — then run it!


  

Were you right? zip() stops at 3 pairs because numbers runs out first.

Dictionary from Two Lists with zip()

Combine zip() with dict() to build a dictionary from two parallel lists — one of keys, one of values:

1
2
3
4
5
keys   = ["name", "age", "city"]
values = ["Alice", 14, "Paris"]

d = dict(zip(keys, values))
print(d)   # {"name": "Alice", "age": 14, "city": "Paris"}

  

*args — Variable Number of Positional Arguments

Sometimes you want a function that can accept any number of arguments. *args collects all extra positional arguments into a tuple.

1
2
3
4
5
6
def total(*args):
    return sum(args)

print(total(1, 2, 3))       # 6
print(total(10, 20))        # 30
print(total(5, 5, 5, 5))    # 20

**kwargs — Variable Number of Keyword Arguments

**kwargs collects extra keyword arguments into a dictionary.

1
2
3
4
5
def describe(**kwargs):
    for key, value in kwargs.items():
        print(f"  {key}: {value}")

describe(name="Monty", species="python", color="green")

  

Aggregating Functions: max(), min(), sum()

These three built-ins work on any iterable of numbers (or comparables for max/min):

1
2
3
4
5
6
scores = [73, 100, 87, 55, 92]

print("Max:", max(scores))    # 100
print("Min:", min(scores))    # 55
print("Sum:", sum(scores))    # 407
print("Avg:", sum(scores) / len(scores))  # 81.4

sorted() and reversed()

sorted(iterable) returns a new sorted list without modifying the original. reversed(iterable) returns an iterator that goes through items in reverse order.

Function Modifies original? Returns
list.sort() Yes None
sorted(list) No New sorted list
list.reverse() Yes None
reversed(list) No Iterator
1
2
3
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
sorted_nums = sorted(numbers)               # ascending
desc_nums   = sorted(numbers, reverse=True) # descending

isinstance() — Type Checking

isinstance(value, type) returns True if the value is the given type (or a subtype). It's safer than using type(x) == int because it handles inheritance.

1
2
3
4
5
print(isinstance(42, int))         # True
print(isinstance(3.14, float))     # True
print(isinstance("hi", str))       # True
print(isinstance([1,2], list))     # True
print(isinstance(42, (int, float)))# True — multiple types allowed

map() and filter()

map(func, iterable) applies a function to every item and returns the results. filter(func, iterable) keeps only items where the function returns True.

Both return iterators — wrap them in list() to see the results:

1
2
3
4
5
6
7
numbers = [1, 2, 3, 4, 5, 6]

doubled  = list(map(lambda x: x * 2, numbers))
evens    = list(filter(lambda x: x % 2 == 0, numbers))

print("Doubled:", doubled)   # [2, 4, 6, 8, 10, 12]
print("Evens:", evens)       # [2, 4, 6]

A lambda is a tiny anonymous function written on one line: lambda x: x * 2 means "take x, return x times 2."


  

Collection Constructors

You can convert between collection types using list(), tuple(), set(), and dict():

1
2
3
my_list  = list(range(5))         # [0, 1, 2, 3, 4]
my_tuple = tuple([1, 2, 3])       # (1, 2, 3)
my_set   = set([3, 1, 4, 1, 5])   # {1, 3, 4, 5}

Learning Check

Your Turn — Use enumerate and zip Together

Monty thinking The code below prints player names and their scores, but the output looks messy. Fix it to use zip() to pair names with scores and enumerate() starting at 1 to number the entries. Each line should look like: 1. Alice: 95


  

Experiments

Try these changes. Predict what will happen first, then run it to check!

  1. Use enumerate(["Mon","Tue","Wed","Thu","Fri"], 1) to print a numbered weekday list. You'll know it worked when you see 1. Mon, 2. Tue, etc.

  2. Write a function my_max(*args) that returns the largest argument without using the built-in max(). You'll know it worked when my_max(3, 9, 1, 7) returns 9.

  3. Use filter() to keep only strings longer than 4 characters from ["cat","elephant","dog","hippo"]. You'll know it worked when you see ["elephant","hippo"].

  4. Use sorted(words, key=len) to sort a list of words by their length (shortest first). You'll know it worked when short words appear before long ones.

  5. Use dict(zip(range(1, 6), ["one","two","three","four","five"])) to build a number-to-word dictionary. You'll know it worked when d[3] prints "three".

Built-in Function Master!

Monty celebrating You've leveled up with enumerate, zip, args, kwargs, map, filter, and more! These tools make Python code shorter, more readable, and more powerful. Professional programmers use every single one of them regularly. Great work!

Take the Chapter Review Quiz

See Annotated References