Python basics, lists, dictionaries, and sets
Practice this question🔴 MUST KNOW · Learn enough to operate AI services; keep existing MERN responsibilities in TypeScript when appropriate.
Overview
- Python is a dynamically typed language widely used by AI libraries.
- A list is an ordered collection, a dictionary maps keys to values, and a set stores unique hashable values.
- Python uses indentation to define code blocks rather than braces.
Why this matters
Many document-processing, model-training, numerical, and evaluation libraries are easiest to use in Python.
An everyday example
A list is a queue of documents; a dictionary is a labeled filing cabinet; a set is a guest list without duplicates.
A technical example
chunks = ["policy", "claim"]; metadata = {"tenant_id": "t1"}; unique_ids = {"C1", "C2"}.
How it works
- Create values
- Use indexing or key lookup
- Loop or use comprehensions
- Pass results to functions.
- Lists and dictionaries are mutable; sharing them can create accidental changes.
Architecture and flow
Parsed documents → lists of chunks → dictionaries of metadata → unique IDs for deduplication.
The insurance assistant example
A worker extracts policy paragraphs, attaches source metadata, and deduplicates document identifiers.
When to use it
For Python-facing data preparation and AI libraries.
When to avoid it
Do not rewrite a working Node service merely to use a hosted LLM API.
Interview answer
“I use Python where its AI ecosystem provides practical benefits and keep collection types explicit so ingestion and evaluation code remain readable.”
Benefits, trade-offs, and production considerations
- TypeScript interfaces and Python type hints do not provide runtime validation.
- Python's
None,True,False,and,or, andnotcorrespond roughly to JS'snull, booleans, and logical operators, but truthiness and equality rules differ.
| TypeScript/JavaScript | Python | Practical difference |
|---|---|---|
const x = null | x = None | Check absence with x is None |
string[] / array | list[str] | Lists may contain mixed types unless validated |
object / Map | dict[str, str] | Dictionary keys must be hashable |
Set<string> | set[str] | Empty set is set(), not {} |
=== | == for value equality | is checks identity, not general equality |
arr.map(fn) | [fn(x) for x in arr] | Comprehensions are common |
try/catch/finally | try/except/finally | Catch specific exceptions |
| template literal | f"claim {claim_id}" | Never use interpolation to construct SQL |
| braces | indentation | Usually four spaces per indentation level |
# No third-party dependencies: basic data preparation.
documents = [
{"id": "p1", "text": "Claim reporting instructions", "active": True},
{"id": "p2", "text": "Old wording", "active": False},
]
active_texts: list[str] = [d["text"] for d in documents if d["active"]]
seen_ids: set[str] = {d["id"] for d in documents}
for index, text in enumerate(active_texts):
print(index, text)