Pivot a list of row dicts into per-column lists by looping over the records and appending each field to its column accumulator. The pandas version passes the same list-of-dicts directly to pd.DataFrame, which performs the same pivot internally and returns a two-dimensional structure with an aligned index.

By hand

With pandas

pd.DataFrame(records) accepts a list of dicts and builds a DataFrame with one column per key. The snapshot shows the column names, shape, and each column's values.

naive.py
# trace: ignore records
records = [
    {'name': 'al', 'age': 20, 'score': 80},
    {'name': 'bo', 'age': 25, 'score': 90},
    {'name': 'cy', 'age': 22, 'score': 70},
    {'name': 'di', 'age': 28, 'score': 85},
]
names = []
ages = []
scores = []
for r in records:
    names.append(r['name'])
    ages.append(r['age'])
    scores.append(r['score'])
print('RESULT:', sorted(['name', 'age', 'score']))
library.py
import pandas as pd
from dalib.display import set_display
set_display()

records = [
    {'name': 'al', 'age': 20, 'score': 80},
    {'name': 'bo', 'age': 25, 'score': 90},
    {'name': 'cy', 'age': 22, 'score': 70},
    {'name': 'di', 'age': 28, 'score': 85},
]
df = pd.DataFrame(records)
print('columns:', df.columns.tolist())
print('shape:', df.shape)
print('name:', df['name'].tolist())
print('age:', df['age'].tolist())
print('score:', df['score'].tolist())
print('RESULT:', sorted(df.columns.tolist()))
columns: ['name', 'age', 'score']
shape: (4, 3)
name: ['al', 'bo', 'cy', 'di']
age: [20, 25, 22, 28]
score: [80, 90, 70, 85]
RESULT: ['age', 'name', 'score']

Implementation notes

  • A DataFrame is a collection of Series columns sharing one index. Each column is a typed 1D array (dtype per column); the shared index aligns rows across columns, just as the record position did in the naive loop.
  • df.shape returns (rows, columns) — the 2D analogue of len() on a Series.
  • pd.DataFrame also accepts a dict of column-lists ({'name': [...], ...}), which mirrors the columnar layout of the hand-written names, ages, scores lists.
  • The records variable is excluded from the trace replay with # trace: ignore records because its repr (160 chars) exceeds the 80-char display limit; each per-record dict r remains visible in the loop.