Pages

Topics

Tools

↑↓navigate↵openescclose

Python guides

Every published Python guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest Python guides

Illustration of a Python asterisk spreading a list of items into separate boxes, representing starred unpacking.
Python

Python Starred Unpacking: Syntax, Uses, and Pitfalls

Learn how Python's starred unpacking works for assignments, function calls, and function definitions. Includes syntax, edge cases, and performance considerations.

unpackingfunction argumentsiterableskwargs
Illustration of a Python list being split into separate variables using unpacking, with a head element separated from a tail group.
Python

Python List Unpacking: Syntax and Practical Patterns

Learn Python list unpacking with practical examples, starred expressions, common errors, and the runtime behavior behind destructuring patterns.

pythonlist unpackingdestructuringstarred expressions
Illustration comparing a compact list comprehension expression with a traditional for loop in Python, showing two paths to the same result.
Python

Python List Comprehension vs Loop: Which to Use?

python list comprehension vs loop: Compare Python list comprehensions and for loops for readability, performance, and memory. Learn when each approach is the better en...

PythonList ComprehensionPerformanceCode Readability
Diagram showing a nested list of values being flattened into a single flat list by a Python list comprehension
Python

Python Nested List Comprehension: Syntax and Examples

Understand Python nested list comprehension syntax, learn to flatten nested lists, apply conditions at any level, and know when a regular loop is better.

list comprehensionnested loopsPython syntaxdata transformation
Diagram showing how if/else in a Python list comprehension either filters elements or transforms values, with a list on the the left and a filtered or transformed list on the the right.
Python

Python List Comprehension if else: Syntax and Use Cases

Python list comprehension if/else: Learn how if/else works inside Python list comprehensions. See filtering vs transforming values, combining both forms, and avoiding common syntax errors.

list comprehensionconditional expressionspython syntaxfiltering
Illustration of Python list comprehension with a conditional filter selecting elements from a list.
Python

Python List Comprehension with If Condition

Use if conditions in Python list comprehensions to filter items, combine conditions, and choose values with if-else. Includes nested examples and readability tips.

list comprehensionconditional filteringpython syntaxgenerator expressions
Illustration of Python list comprehension syntax with a list and transformation arrow
Python

Python List Comprehension Syntax Explained

Understand Python list comprehension syntax with practical examples, conditionals, nested loops, and clear guidance on when a generator expression is a better choice.

list comprehensionpython syntaxgenerator expressionsnested loops
A visual metaphor of Python list comprehension transforming a sequence of items through a filter into a new list.
Python

Python List Comprehension: Syntax, Filtering, and Performance

Learn Python list comprehension syntax, conditional filtering, nested loops, and when a generator expression is a more memory-efficient alternative.

PythonList ComprehensionPython SyntaxGenerator Expressions
Illustration of a nested list structure being flattened into a single list in Python, showing layers of brackets collapsing into a linear sequence.
Python

How to Flatten a Nested List in Python

Learn how to flatten nested lists in Python using list comprehensions, itertools.chain, recursion, generators, and an iterative stack. Compare readability, memory use, and when each approach is appropriate.

list flatteningrecursionitertoolslist comprehension
Diagram of a two-dimensional list in Python showing rows and columns with index labels.
Python

Python Two Dimensional List: Creation and Manipulation

Learn how to create, index, and iterate over two-dimensional lists in Python, including common aliasing pitfalls and when to use NumPy instead.

nested listslist-of-listsmatrixpython data structures