โณ Loading Python Engine...

๐Ÿ“Š Day 12 : Comprehensions

๐ŸŽฏ Enterprise Objective

Comprehensions are the signature of Pythonic code. Today we move beyond simple lists to master nested data manipulation, ternary mappings, and O(1) dictionary generations. These techniques drastically reduce lines of code and improve execution speed in data processing pipelines.

๐Ÿ“‹ Strategic Overview

#TopicConcept
1Advanced ListsNested loops, Ternaries
2Dicts & Sets{}, k:v mapping

1. Advanced List Comprehensions : Deep Dive

๐Ÿ” What is it?

We've seen basic list comprehensions. Advanced comprehensions allow for nested loops and if/else conditionals. They are the fastest, most Pythonic way to filter and transform lists.

# If/Else mapping (Ternary inside comprehension)
results = ['Pass' if x >= 50 else 'Fail' for x in scores]

# Nested Loops (Flattening a matrix)
flat = [item for row in matrix for item in row]

๐Ÿ’ผ Why Data Analysts Care

โ€ข Data Cleaning: Instantly mapping raw values to standardized categories

โ€ข Matrix Flattening: Converting a list-of-lists (JSON structure) into a flat 1D list

โš ๏ธ Readability Trap

Never nest more than two loops in a comprehension. If it takes more than 5 seconds to read, convert it to standard for loops.

In [ ]:

๐Ÿงช Concept Checks: Adv List Comp

Q1. Given matrix = [[1,2,3], [4,5,6]], write a comprehension to flatten it into [1,2,3,4,5,6]. Print it.

In [ ]:

Q2. Use a comprehension with an if/else ternary to map data = [-1, 2, -3, 4] to ["Neg", "Pos", "Neg", "Pos"].

In [ ]:

Q3. Generate all Cartesian coordinates for x in [0, 1] and y in [0, 1] using a nested comprehension resulting in [(0,0), (0,1), (1,0), (1,1)].

In [ ]:

Q4. Given names = ["alice", "bob", ""], use a comprehension to capitalize valid names and drop empty strings.

In [ ]:

Q5. Why is [x if x > 0 for x in data] invalid syntax? Explain the difference between filtering and mapping syntax.

In [ ]:

2. Dict & Set Comprehensions : Dynamic Hash Maps

๐Ÿ” What is it?

Just like lists, you can create Dictionaries and Sets using comprehensions. For dictionaries, use the key: value syntax inside curly braces {}. For sets, just use values inside curly braces.

# Dict Comprehension (Reverse mapping)
lookup = {value: key for key, value in original.items()}

# Set Comprehension (Unique lengths)
lengths = {len(word) for word in words}

๐Ÿ’ผ Why Data Analysts Care

โ€ข Fast Lookups: Convert a list of user objects into a dictionary keyed by ID for O(1) lookups

โ€ข Deduplication: Extracting a set of unique domains from a list of email strings

๐Ÿง  Pro Tip

When swapping keys and values using {v: k for k,v in d.items()}, remember that if values are not unique, the last one processed will overwrite the others!

In [ ]:

๐Ÿงช Concept Checks: Dict/Set Comp

Q1. Given words = ["data", "science", "python"], create a dict mapping each word to its first letter. Print it.

In [ ]:

Q2. Create a set comprehension of the squares of numbers from 1 to 10 that are divisible by 3. Print it.

In [ ]:

Q3. Given d = {"a": 10, "b": -5, "c": 20}, create a new dict containing only positive values.

In [ ]:

Q4. Swap keys and values for codes = {"NY": "New York", "CA": "California"} using a dict comp.

In [ ]:

Q5. Create a dict comprehension combining keys=["x", "y"] and values=[1, 2] using zip().

In [ ]:

๐Ÿ› ๏ธ Professional Practice Tasks

Theory is useless without muscle memory. Complete these tasks to solidify your understanding.

Task 1 (Matrix Transpose): Given a 3x3 matrix [[1,2,3], [4,5,6], [7,8,9]], use a nested list comprehension to transpose it (swap rows and columns) into [[1,4,7], [2,5,8], [3,6,9]].

In [ ]:

Task 2 (Frequency Map): Given a string text = "hello world", use a dict comprehension with text.count() to create a frequency map of characters. (Note: this is O(n^2), but good practice).

In [ ]:

Task 3 (Data Cleaner): Given a list of strings raw = [" 10 ", "20", " ", "30 "], use a list comprehension to strip whitespace and convert to integers, skipping empty/blank strings.

In [ ]:

Task 4 (ID Lookup): Given users = [{'id': 101, 'name': 'Alice'}, {'id': 102, 'name': 'Bob'}], create a dict comp that maps id -> name for fast O(1) lookups.

In [ ]:

Task 5 (Vowel Set): Given a long paragraph, use a set comprehension to extract all unique words that start with a vowel (a, e, i, o, u).

In [ ]:

๐Ÿ’ป Pure Coding Interview Questions

Q1.

Write a list comprehension to generate the first 20 even numbers.

In [ ]:

Q2.

How do you flatten a 2D list [[1,2], [3,4]] using a list comprehension?

In [ ]:

Q3.

Explain the difference in syntax between a list comprehension and a generator expression.

In [ ]:

Q4.

Write a dict comprehension that maps numbers 1-10 to their binary string representation.

In [ ]:

Q5.

Given dict1 = {'a': 1} and dict2 = {'b': 2}, combine them using a dict comprehension (or ** unpacking).

In [ ]:

Q6.

What happens in a list comprehension if the if condition is placed before the for loop?

In [ ]:

Q7.

Write a comprehension that filters out all vowels from a string.

In [ ]:

Q8.

Use a set comprehension to find the common elements between two lists without using set(a) & set(b).

In [ ]:

Q9.

Write a dict comprehension to invert a dictionary d, handling non-unique values by mapping to a list of keys.

In [ ]:

Q10.

Given a list of strings, write a comprehension that returns a list of lengths, but only for strings longer than 3 characters.

In [ ]:

Q11.

Write a nested dictionary comprehension to create a multiplication table (1 to 5).

In [ ]:

Q12.

Is a list comprehension faster than a standard for loop with .append()? Why?

In [ ]:

Q13.

Write a comprehension that parses a list of strings formatted as 'key:value' into a dictionary.

In [ ]:

Q14.

Explain the memory implications of using a list comprehension vs a generator expression on 10 million rows.

In [ ]:

Q15.

Write a list comprehension that implements the FizzBuzz logic for numbers 1 to 50.

In [ ]:

Q16.

Given a list of dictionaries, write a comprehension to extract the value of the 'name' key, providing 'Unknown' if missing.

In [ ]:

Q17.

Write a comprehension to find all prime numbers up to 100 (can use an external is_prime function).

In [ ]:

Q18.

Transpose a matrix without using list comprehensions (using zip(*matrix)). Which is better?

In [ ]:

Q19.

Write a dict comprehension that maps characters of a string to their ASCII integer values.

In [ ]:

Q20.

Why shouldn't you use a list comprehension just for its side effects (like printing)?

In [ ]:

Q21.

Write a comprehension to extract the diagonal elements of a square matrix.

In [ ]:

Q22.

Create a list of all possible 2-character combinations from the alphabet using a nested comprehension.

In [ ]:

Q23.

Given prices = {'apple': 1.0, 'banana': 0.5}, use a dict comp to apply a 10% discount to all items.

In [ ]:

Q24.

Write a list comprehension that extracts numbers from a string s = 'abc123def456'.

In [ ]:

Q25.

Refactor a 3-level deep nested loop into a comprehension. Then explain why you probably shouldn't do that in production code.

In [ ]:

๐Ÿ“Š Day 12 Executive Summary

#TopicKey Takeaway
1Lists[val if cond else alt for x in data]
2Dicts{row['id']: row for row in data} for fast lookups

โœ… Instructor's End-of-Day Checklist

โ€ข [ ] I can write a list comprehension with an if/else ternary.

โ€ข [ ] I can flatten a matrix using nested loops in a comprehension.

โ€ข [ ] I can create a dictionary dynamically.