SHOW COLUMNS or Get Column Names From SQLalchemy SELECT Query

Updated 2026-09-26: updated the example for SQLAlchemy 2.0.

http://stackoverflow.com/questions/8947616/python-sqlalchemy-get-column-names-dynamically

Rob Wouter’s answer really helped me out: result.keys() gives you the column names, and each row’s columns and values are on row._mapping (SQLAlchemy 2.0 moved them off the row itself):

from sqlalchemy import create_engine, text

engine = create_engine("sqlite:///example.db")

with engine.connect() as conn:
    result = conn.execute(text("SELECT * FROM users"))
    print(list(result.keys()))  # ['id', 'name', 'email']

    for row in result:
        for column, value in row._mapping.items():
            print(f"{column}: {value}")