Collections: Beginner
Python's collections module ships with data structures that replace dozens of lines of manual code with a single import. Counter objects at companies like Mozilla count event frequencies across millions of records in one line, and namedtuples make tuple data self-documenting without the overhead of a full class. Palantir's data engineers use defaultdict to accumulate grouped results without checking whether a key exists on every iteration. The collections module tools in this lesson are the professional Python developer's first upgrade beyond basic lists and dicts.
Creating Tuples
Build immutable sequences for safe data
A tuple is an ordered, immutable sequence of values. You create tuples using parentheses () instead of the square brackets [] used for lists. The values inside can be of any type, and you can mix types freely just like with lists.
Basic Tuple Creation
Tuples vs Lists: Key Diff
- Data should not change
- Representing fixed records
- Dictionary keys needed
- Returning multiple values
- Coordinates or dimensions
- Data will be modified
- Building up collections
- Order may change
- Adding/removing items
- Sorting or shuffling needed
Single-Element Tuples
Converting Tuples and Lists
You can convert between tuples and lists using tuple() and list(). This is useful when you need to modify data that arrived as a tuple, or when you need to make a list immutable:
> You need to store a single value, 42, as a tuple rather than an integer. Pick the syntax that actually creates a tuple instead of just grouping.
single = print(type(single))
> You need to change the first coordinate of an immutable tuple. Choose the right conversions: one to make it modifiable, and one to freeze the result back.
point = (10, 20) coords = (point) coords[0] = 15 result = (coords) print(result)
Tuple Unpacking
Extract multiple values in one line
Basic Unpacking
Swapping Variables
The expression x, y = y, x works because Python evaluates the right side completely before assigning to the left side. It creates a temporary tuple (y, x) and then unpacks it into x and y.
Unpacking in Loops
Unpacking with enumerate()
The built-in enumerate() function pairs each item with its index, returning tuples. Combined with unpacking, it gives you both the index and value in a clean way:
Ignoring Values: Underscore
Sometimes you only need some values from a tuple. By convention, Python programmers use underscore _ for values they want to ignore:
The underscore _ is a valid variable name, but it signals to readers "I am intentionally ignoring this value." This convention makes your intentions clear and helps code reviewers understand what you actually care about.
> This code tries to unpack a 3-element tuple into only 2 variables, causing a ValueError because the number of variables does not match.
ValueError: too many values to unpack (expected 2)
Using min, max, sum
Summarize any numeric collection instantly
Python provides three essential built-in functions for working with numeric collections: min() finds the smallest value, max() finds the largest value, and sum() totals all values. These functions work on any iterable containing comparable values - lists, tuples, sets, and more. They are so fundamental that Python includes them as built-in functions available everywhere without any imports.
Data engineers use these functions constantly. What is the earliest timestamp in a log file? Use min(). What is the highest transaction amount today? Use max(). What is the total revenue? Use sum(). These operations are so fundamental that Python makes them available as built-in functions rather than requiring imports.
Finding Minimum and Maximum
The min() and max() functions scan through a collection and return the smallest or largest value. They work with numbers, strings, and any other comparable types:
Summing Values
The sum() function adds all values in a collection. It works with any numeric types and is much cleaner than writing a loop:
The optional second argument to sum() specifies a starting value. This is useful when you want to add to an existing total rather than starting from zero. The default starting value is 0.
Combining min, max, sum
default with min and max
To safely handle empty collections, use the default parameter with min() and max():
> You have a list of five test scores and need to compute a summary statistic. Pick the built-in function to apply and see what it returns.
scores = [85, 92, 78, 95, 88] result = (scores) print(result)
These three functions cover the most common aggregate operations in data analysis: sum() for totals, min() and max() for range, and combining them with len() for averages.
min() and max() support a key parameter just like sorted(), allowing you to find the minimum or maximum based on a computed value rather than the raw element.
For empty collections, sum() safely returns 0, but min() and max() raise ValueError. Use the default parameter to handle empty inputs gracefully in production code.
> Compute the average of five test scores. Choose the function that totals all values for the numerator, and the function that counts items for the denominator.
scores = (85, 92, 78, 95, 88) average = (scores) / (scores) print(average)
Computing averages with sum() and len() is a fundamental pattern. For large datasets, this single-pass approach is more efficient than sorting and picking the middle value.
abs() for Absolutes
Measure distances and errors correctly
The abs() function returns the absolute value of a number - its distance from zero on the number line. For positive numbers, abs() returns the same value. For negative numbers, it removes the negative sign. This function is essential when you care about magnitude but not direction.
Basic Absolute Value
Practical Applications
> This code calculates the distance between two points but gets a negative result because it subtracts without taking the absolute value.
Logic error: distance should always be positive, but the output is -15
abs() is essential whenever you care about magnitude rather than direction. Distances, deviations, and error measurements should always be non-negative.
The pattern abs(a - b) is symmetric: it produces the same result regardless of which value is a and which is b. This makes it the correct way to compute unsigned differences.
For floating-point comparisons, checking abs(x - y) < tolerance is the standard approach because direct equality fails due to rounding errors in how computers represent decimals.
len() Across Types
Check size of any collection reliably
The len() function returns the number of items in a collection. It works uniformly across all Python sequence and collection types: lists, tuples, strings, dictionaries, sets, and more. This consistency is one of Python's design strengths.
You have probably used len() with lists already. This section explores how it works across different types and shows important patterns for using it effectively. Understanding len() deeply helps you write more robust code that handles edge cases properly. Knowing when and how to check collection size is essential for writing defensive code that handles unexpected inputs gracefully.
len() with Different Types
The len() function works consistently across all built-in collection types:
Notice that for dictionaries, len() returns the number of key-value pairs, not the total of keys plus values. For sets, it counts unique elements after duplicates are removed. This consistent behavior makes len() predictable across all collection types. Once you understand how len() works, you can apply that knowledge to any collection you encounter.
For strings, len() counts characters including spaces and punctuation. This is important for data validation - checking that a username is between 3 and 20 characters, ensuring a description is not too long, or validating that a required field is not empty.
Checking Empty Collections
A common use of len() is checking if a collection is empty. However, Python has a more idiomatic way to do this - empty collections are "falsy" and non-empty collections are "truthy":
len() in Common Patterns
len() on Nested Structures
> You have a nested list [[1, 2], [3, 4], [5, 6]] and need to count its elements. Pick the expression that returns the count you expect.
data = [[1, 2], [3, 4], [5, 6]] result = print(result)
Common Mistakes
- Use trailing comma for single-element tuples: (42,)
- Use default= parameter with min() and max() on possibly empty data
- Match variable count exactly when unpacking tuples
- Try to modify tuple elements (they are immutable)
- Use len() > 0 instead of the Pythonic if collection:
- Confuse tuple parentheses with function call parentheses
Single-Element Tuple Error
Unpacking Mismatch Mistake
> You are a data analyst at Shopify auditing a product catalog migration. You must verify that every record transferred correctly, identify the longest and shortest SKU codes, confirm the total item count, and flag any entries whose price deviates below zero after a currency conversion.
Tuples and essential built-in functions
- Category
- Python
- Difficulty
- beginner
- Duration
- 34 minutes
- Challenges
- 0 hands-on challenges
Topics covered: Creating Tuples, Tuple Unpacking, Using min, max, sum, abs() for Absolutes, len() Across Types
Lesson Sections
- Creating Tuples (concepts: pyTuples)
The word "tuple" comes from mathematics, where it describes a finite ordered sequence of elements. A "pair" is a 2-tuple, a "triple" is a 3-tuple, a "quadruple" is a 4-tuple, and so on. In Python, tuples can have any number of elements, from zero to millions. The generic term "n-tuple" refers to a tuple of any length. This mathematical heritage gives tuples a formal, structured character that lists lack. Data engineers encounter tuples constantly. Database query results often come as sequences o
- Tuple Unpacking (concepts: pyUnpacking)
Tuple unpacking is one of Python's most elegant features. It allows you to assign multiple variables from a tuple in a single statement. Instead of accessing each element by index, you can extract all values at once into named variables. This makes code more readable and expressive. When you see unpacking in code, you immediately understand the structure of the data being processed. Data engineers use tuple unpacking constantly. When a function returns multiple values, when iterating over pairs
- Using min, max, sum (concepts: pyMathOps)
Finding Minimum and Maximum Notice that min() and max() can take either a single collection (list, tuple, etc.) or multiple individual arguments. When comparing strings, they use alphabetical (lexicographic) order, where uppercase letters come before lowercase. This makes them useful for finding the first or last item when data is sorted alphabetically. The flexibility to accept either a collection or individual arguments makes these functions convenient in many contexts. These functions are ext
- abs() for Absolutes (concepts: pyMathOps)
Data engineers use abs() when calculating differences, measuring errors, and working with coordinates. If you want to know how far apart two values are regardless of which is larger, you need absolute value. If you want to know the magnitude of a change regardless of direction, you need absolute value. This function appears frequently in validation logic, error calculations, and distance measurements. Basic Absolute Value The abs() function works with integers, floats, and complex numbers: Pract
- len() Across Types (concepts: pyCollections)
len() with Different Types Checking Empty Collections len() in Common Patterns Here are practical patterns using len() that appear frequently in data engineering code: The batch processing example shows how len() helps divide work into manageable chunks. The validation example shows how len() ensures data has the expected structure before processing. Both patterns are common in real-world data pipelines. Whether you are processing millions of records or validating user input, len() is your first