AI EngineeringInterview preparation
Foundation·Concept·3 min read

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

  1. Create values
  2. Use indexing or key lookup
  3. Loop or use comprehensions
  4. 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, and not correspond roughly to JS's null, booleans, and logical operators, but truthiness and equality rules differ.
TypeScript/JavaScriptPythonPractical difference
const x = nullx = NoneCheck absence with x is None
string[] / arraylist[str]Lists may contain mixed types unless validated
object / Mapdict[str, str]Dictionary keys must be hashable
Set<string>set[str]Empty set is set(), not {}
===== for value equalityis checks identity, not general equality
arr.map(fn)[fn(x) for x in arr]Comprehensions are common
try/catch/finallytry/except/finallyCatch specific exceptions
template literalf"claim {claim_id}"Never use interpolation to construct SQL
bracesindentationUsually four spaces per indentation level
Python
# 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)
Adapted from 13_AI.MD · “Python basics, lists, dictionaries, and sets”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.