LINQ to Comprehensions
LINQ to Comprehensions
Introduction
In this lesson, you'll learn about linq to comprehensions in Python. Coming from C#, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C#, you're familiar with linq to comprehensions.
Python has its own approach to linq to comprehensions, which we'll explore step by step.
The Python Way
Let's see how Python handles this concept. Here's a typical example:
# List comprehension (filter + transform)
evens = [n * 2 for n in numbers if n % 2 == 0]
# Dict comprehension
squares = {n: n**2 for n in range(10)}
# Set comprehension
unique_lengths = {len(s) for s in strings}
# Generator (lazy, like unevaluated LINQ)
gen = (n * 2 for n in numbers if n % 2 == 0)Comparing to C#
Here's how you might have written similar code in C#:
// Filter + transform
var evens = numbers
.Where(n => n % 2 == 0)
.Select(n => n * 2)
.ToList();
// Query syntax
var query = from n in numbers
where n > 0
select n * n;You may be used to different syntax or behavior.
List comprehensions replace LINQ Where + Select + ToList() in one line
You may be used to different syntax or behavior.
Comprehension syntax: [expression for item in iterable if condition]
You may be used to different syntax or behavior.
Python also has dict comprehensions {} and set comprehensions {}
You may be used to different syntax or behavior.
Generator expressions use () and are lazy, like un-materialized LINQ
You may be used to different syntax or behavior.
For complex pipelines, chained calls to filter() and map() also work
Step-by-Step Breakdown
1. Basic List Comprehension
A list comprehension combines filter and transform into a single readable expression. The condition at the end is optional.
var doubled = numbers
.Where(n => n % 2 == 0)
.Select(n => n * 2)
.ToList();doubled = [n * 2 for n in numbers if n % 2 == 0]
# Read as: "n*2 for each n in numbers where n is even"2. Dict and Set Comprehensions
The same syntax extends to dicts and sets. Dict comprehensions use key: value syntax inside {}.
var squared = numbers
.ToDictionary(n => n, n => n * n);squared = {n: n**2 for n in numbers}
# Set comprehension (unique values)
lengths = {len(word) for word in words}3. Nested Comprehensions
Comprehensions can be nested for multi-dimensional data — like nested LINQ SelectMany.
var flat = matrix
.SelectMany(row => row)
.ToList();# Flatten a 2D list
flat = [x for row in matrix for x in row]
# Equivalent with nested loops:
flat = []
for row in matrix:
for x in row:
flat.append(x)4. Generators — Lazy Evaluation
Generator expressions use () instead of []. They are lazy — values are computed on demand, exactly like non-materialized LINQ queries.
// C# - lazy LINQ (not yet evaluated)
IEnumerable<int> lazy = numbers.Where(n => n > 0);
// Evaluated only when iterated# Generator expression — lazy
lazy = (n * 2 for n in numbers if n > 0)
# Iterating triggers computation:
for val in lazy:
print(val)
# Or materialize:
result = list(lazy)Common Mistakes
When coming from C#, developers often make these mistakes:
- List comprehensions replace LINQ Where + Select + ToList() in one line
- Comprehension syntax: [expression for item in iterable if condition]
- Python also has dict comprehensions {} and set comprehensions {}
Key Takeaways
- [expr for x in iterable if cond] replaces Where + Select + ToList()
- Dict {k: v for ...} and set {v for ...} comprehensions follow the same pattern
- Use () for lazy generator expressions (like non-materialized LINQ)
- Keep comprehensions short — use a for loop when readability suffers