Derive row count, column count, and sorted column names from a list of records using len and a key-extraction loop. With pandas, df.shape and df.columns expose the same facts directly as attributes on the DataFrame.

By hand

Count rows with len(records). Extract column names by looping over the keys of the first record and collecting them into cols. Sort to get a stable order.

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},
]
n_rows = len(records)
cols = []
for k in records[0]:
    cols.append(k)
cols = sorted(cols)
n_cols = len(cols)
print('RESULT:', (n_rows, n_cols, cols))

With pandas

df.shape returns a (rows, cols) tuple. df.columns is an Index of column labels; .tolist() converts it to a plain Python list.

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)
cols = sorted(df.columns.tolist())
print('shape:', df.shape)
print('columns:', df.columns.tolist())
print('RESULT:', (df.shape[0], df.shape[1], cols))
shape: (4, 3)
columns: ['name', 'age', 'score']
RESULT: (4, 3, ['age', 'name', 'score'])

Implementation notes

  • df.shape is a property, not a method — no parentheses. It returns a plain Python tuple, so df.shape[0] (row count) and df.shape[1] (column count) work without any conversion.
  • df.columns is a pandas Index object, not a list. It supports label lookup, set operations (union, intersection), and alignment — calling .tolist() drops those capabilities and gives a plain Python list for stable comparison or printing.
  • Column order reflects insertion order (the key order of the first record when built from a list-of-dicts). sorted() gives a deterministic order for comparison, as in the naive loop.
  • df.dtypes gives the dtype of every column at once; df.info() gives shape, column names, dtypes, and memory usage in one call.