Reference library

Python Code Samples

Medium snippets you can copy, study, and run in the browser editor.

2 matches
Files & data medium

How to Build a CSV Comparison Tool That Highlights Every Changed Cell in Python

Read two CSV files with DictReader, compare cell by cell, and return a list of dictionaries describing each changed cell using only the standard library.

csv comparison diff
Python
import csv
from pathlib import Path

def csv_cell_diff(file_a: str, file_b: str) -> list[dict]:
    rows_a = list(csv.DictReader(Path(file_a).open('r', newline='')))
    rows_b = list(csv.DictReader(Path(file_b).open('r', newline='')))
    if not rows_a or not rows_b:
        return []
    columns = list(rows_a[0].key…
40 0 Open
Files & data medium

Join two CSV files on shared key column in Python

Merge rows from two CSV files by a common key column, outputting combined records to a new file.

csv join dictreader
Python
import csv

def join_csv(file1, file2, key, output="joined.csv"):
    # Read first CSV into dict keyed by the join column
    with open(file1, newline="") as f1:
        reader1 = csv.DictReader(f1)
        data1 = {row[key]: row for row in reader1}

    # Read second CSV and merge matching rows
    with open(file2, n…
15 0 Open

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

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. 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.