Python Code
Samples
Easy snippets you can copy, study, and run in the browser editor.
How to Build a Gradebook with Python Dictionaries and Sets
Create a gradebook dictionary from student names and grades, find top students with a set comprehension, and add extra credit with a dict comprehension.
def build_gradebook(students, grades):
"""Create a dictionary mapping student names to their grades."""
return dict(zip(students, grades))
def find_top_students(gradebook, passing_grade=60):
"""Return a set of students with grades at or above the passing grade."""
return {name for name, grade in grad…
How to Extract Data by Category in Python with Dictionaries and Sets
Use set comprehensions and a defaultdict to extract product names by category and compute total prices per category from a list of dictionaries.
from collections import defaultdict
# Sample data: products with categories and prices
product_data = [
{"name": "Apple", "category": "fruit", "price": 0.50},
{"name": "Banana", "category": "fruit", "price": 0.30},
{"name": "Carrot", "category": "vegetable", "price": 0.80},
{"name": "Bread", "category…
Convert Data in Python with Comprehensions and Generators
Convert mixed data to integers, filter and transform numbers, and extract fields from dicts using list comprehensions and generator expressions.
def convert_numbers(data):
"""Convert a list of mixed values into integers using a comprehension."""
return [int(item) for item in data if item is not None]
def double_even_numbers(numbers):
"""Double only even numbers using a generator expression."""
return (n * 2 for n in numbers if n % 2 == 0)
d…
Count Data in Python with Comprehensions and Generators
Count list items with a dict comprehension and generate squares lazily with a generator expression, printing both results.
from collections import Counter
data = ["apple", "banana", "apple", "cherry", "banana", "apple"]
counts = {item: data.count(item) for item in set(data)}
square_gen = (x * x for x in range(5))
squares = list(square_gen)
if __name__ == "__main__":
print("Manual count:", counts)
print("Counter:", dict(Counter…
Generate Data with Python Comprehensions and Generators
Shows list, dict compregensions and generator expressions plus a Fibonacci generator to produce data lazily.
# Data generation helpers using comprehensions and generators
from itertools import islice
def fibonacci(limit):
"""Generate Fibonacci numbers up to a limit."""
a, b = 0, 1
while a <= limit:
yield a
a, b = b, a + b
def main():
# List comprehension: squares of even numbers
square…
How to Filter Data with Predicates in Python
This helper filters a list with a predicate using a list comprehension, plus a lazy generator version that yields matches one by one.
def filter_data(data, predicate):
"""Return a list containing only items that pass the predicate."""
return [item for item in data if predicate(item)]
def filter_data_lazy(data, predicate):
"""Generator version: yields items that pass the predicate one by one."""
for item in data:
if predicat…
How to Group Data in Python with defaultdict and Comprehensions
Group a list of items by a computed key using a defaultdict-based generator helper and an alternative dictionary comprehension approach.
from collections import defaultdict
def group_by(data, key_func):
"""Group items in data by the value returned by key_func."""
result = defaultdict(list)
for item in data:
result[key_func(item)].append(item)
return dict(result)
def group_by_comprehension(data, key_func):
"""Same grouping …
How to Parse Data with Generators and Comprehensions in Python
This code demonstrates using a generator expression to filter active users and a dictionary comprehension to aggregate scores by name.
def parse_data_helper(raw_records):
"""Extract active users' names and scores from raw records."""
parsed = (
(record["name"], record["score"])
for record in raw_records
if record["active"] and record["score"] >= 0
)
return list(parsed)
def aggregate_scores(parsed_data):
"…
How to Sort Data with Comprehensions and Generators in Python
Sort a list of tuples by a key, then use a list comprehension to extract names and a generator to square high ranks.
data = [("Anna", 3), ("Ben", 1), ("Clara", 2), ("Dan", 5), ("Eve", 4)]
# Comprehension: list of tuples (name, rank) sorted ascending by rank
sorted_by_rank = sorted(data, key=lambda x: x[1])
# Comprehension: extract just the names in rank order
names_in_rank_order = [name for name, rank in sorted_by_rank]
# Generat…
How to Split Data into Chunks and Use Generators in Python
Split a list into fixed-size chunks with a list comprehension and square even numbers lazily with a generator expression.
def split_numbers(data, chunk_size):
return [data[i:i + chunk_size] for i in range(0, len(data), chunk_size)]
def square_even_numbers(numbers):
return (n ** 2 for n in numbers if n % 2 == 0)
if __name__ == "__main__":
sample_data = list(range(1, 21))
chunks = split_numbers(sample_data, 5)
print…
How to Use Comprehensions and Generators in Python
Demonstrate list, set, and dictionary comprehensions plus generator expressions and generator functions in one beginner-friendly script.
def demonstrate_comprehensions_generators():
# List comprehension: transform and filter in one line
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares = [num ** 2 for num in numbers if num % 2 == 0]
print(f"Square of even numbers (list comprehension): {squares}")
# Set comprehension: unique values
…
How to Use Comprehensions and Generators to Check Data in Python
A beginner-friendly helper that filters numeric values, computes squares and cubes with comprehensions and a generator, and returns a summary dictionary.
def check_data(iterable):
"""Return a summary of numeric data using comprehensions and a generator."""
values = [item for item in iterable if isinstance(item, (int, float))]
squares = [x ** 2 for x in values if x > 0]
cubes = (x ** 3 for x in values if x > 0)
cube_list = list(cubes)
return {
…
How to Use List Comprehensions and Generators in Python
Analyze a list of numbers using a list comprehension to square evens, a generator for sum, and a generator expression for the maximum squared value.
def analyze_numbers(numbers):
squared = [n ** 2 for n in numbers if n % 2 == 0]
total = sum(n for n in numbers)
max_squared = max((n ** 2 for n in numbers), default=0)
return squared, total, max_squared
if __name__ == "__main__":
data = [1, 2, 3, 4, 5, 6]
evens_squared, total_sum, max_sq = an…
How to Use List Comprehensions and Generators to Format Data in Python
A beginner-friendly helper that formats dictionaries into strings using a list comprehension and generates squared numbers lazily with a generator.
def format_data(items):
"""Format a list of dictionaries into readable strings."""
formatted = [
f"{item.get('name', 'Unknown')}: {item.get('value', 0)} units"
for item in items
if item.get('value', 0) > 0
]
return formatted if formatted else ["No positive values found"]
def g…
How to Use List Comprehensions and Generators to Transform Data in Python
Transform a list of integers by squaring even numbers with a list comprehension and cubing odd numbers with a generator.
def transform_data(data):
"""
Transform a list of integers:
- squares of even numbers using a list comprehension
- cubes of odd numbers using a generator
"""
squares = [num ** 2 for num in data if num % 2 == 0]
cubes = (num ** 3 for num in data if num % 2 != 0)
return squares, cubes
i…
How to Validate Data with Python Comprehensions and Generators
Use list, generator, and dictionary comprehensions to filter and transform data for quick validation in Python.
def validate_integer(data):
return [item for item in data if isinstance(item, int)]
def validate_positive(numbers):
return (num for num in numbers if num > 0)
def validate_string_lengths(data, min_length=3):
return {item: len(item) for item in data if isinstance(item, str) and len(item) >= min_length}
i…
Normalize Data in Python with Comprehensions and Generators
Clean a list by dropping None values with a comprehension, then min-max normalize it using a lazy generator expression — a beginner-friendly data preparation pattern.
import statistics
# Sample raw data including missing and outlier-ish values
raw = [22, 18, None, 25, 30, 19, 22, 17, None, 28, 24]
# Clean the data: drop None values using a list comprehension
clean = [x for x in raw if x is not None]
# Normalize using min-max scaling with a generator expression
min_val = min(clea…
Python Comprehensions and Generators for Beginners
Learn list, dict, and set comprehensions plus generator expressions and generator functions with clear, runnable examples.
# Demonstrates list comprehensions, dict comprehensions, set comprehensions, and generators
def demonstrate_comprehensions():
# List comprehension: squares of even numbers
numbers = range(1, 11)
even_squares = [n ** 2 for n in numbers if n % 2 == 0]
# Dict comprehension: number to its factorial
…
Write Data Helpers with Comprehensions and Generators in Python
Demonstrates list, dict, and set comprehensions plus generator expressions and generator functions for building concise data helpers.
# Basic comprehensions and generators demo
# List comprehension: squares of evens
squares = [x * x for x in range(10) if x % 2 == 0]
print("List comp:", squares)
# Dictionary comprehension: char -> count
text = "hello"
char_counts = {c: text.count(c) for c in set(text)}
print("Dict comp:", char_counts)
# Set compre…
How to Filter Data in Python
Filter a list of dictionaries by exact key-value matches or numerical ranges using concise list comprehensions.
from typing import List, Dict, Any
def filter_data(
data: List[Dict[str, Any]], key: str, value: Any
) -> List[Dict[str, Any]]:
"""Return records where data[key] equals value."""
return [record for record in data if record.get(key) == value]
def filter_by_range(
data: List[Dict[str, Any]], key: str…
Browse by section
Each section groups closely related Python snippets.
Guide: free Python code samples library
Copy-ready Python snippets for learners and developers
PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.
How to use this library
- Pick a topic section — strings, lists, files, functions, and more
- Open a sample, read How it works, and copy the code block
- Run it in the IDE, tweak values, then take a related quiz or tutorial lesson
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.