Python SQLAlchemy - Get column names dynamically Last Updated : 20 Feb, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to dynamically get the column names of a table in SQLAlchemy using Python. Used database for demonstration:Student table So, our student table has 5 columns namely sno, name, dob, class, and section, and our task is to fetch all these column names in our Python code. First of all, we will import the sqlalchemy module, create an engine and then create a connection with the database. Then we will execute a query on the table whose column names we want. Example 1: Now using the keys method of that result object obtained by running the query we can get all the column names dynamically. Python3 from sqlalchemy import create_engine table_name = 'student' engine = create_engine("mysql+pymysql://root:root123@localhost/geeksforgeeks") connection = engine.connect() result = connection.execute(f"SELECT * FROM {table_name}") print(result.keys()) Output: Output of above codeExample 2: We can also use the result.cursor.description of the result object. The result.cursor.description is a list containing tuples whose 1st element is the name of the column. Let us run a for loop on it and store the first element of it in our custom columns variable. Python3 from sqlalchemy import create_engine table_name = 'student' engine = create_engine("mysql+pymysql://root:root123@localhost/geeksforgeeks") connection = engine.connect() result = connection.execute(f"SELECT * FROM {table_name}") columns = [] for elem in result.cursor.description: columns.append(elem[0]) print(columns) Output of above code Comment More infoAdvertise with us Next Article Python SQLAlchemy - Get column names dynamically mycodenotein Follow Improve Article Tags : Python Geeks Premier League Geeks-Premier-League-2022 Python-SQLAlchemy Practice Tags : python Similar Reads Dynamic Column Parameterization in SQLAlchemy Core SQLAlchemy Core is an adaptable SQL toolkit and Object Relational Mapping (ORM) library, for Python. It is specifically designed to integrate with databases offering an extensive set of functionalities, for creating, querying, and manipulating database tables and records. When working with databases 6 min read How to get column names from SQLAlchemy? In this article, we will discuss how to get column names using SQLAlchemy in Python. SQLAlchemy is an open-source SQL toolkit and object-relational mapper for the Python programming language released under the MIT License. It gives full power and flexibility of SQL to an application. To follow along 3 min read Python SQLAlchemy - func.count with filter In this article, we are going to see how to perform filter operation with count function in SQLAlchemy against a PostgreSQL database in python Count with filter operations is performed in different methods using different functions. Such kinds of mathematical operations are database-dependent. In Po 3 min read Python Falcon - SQLAlchemy Models Python Falcon is an up-to-date web framework with an adjustable applicative architecture that is oriented to creating high-speed application processes and APIs. Another often implemented pattern is CRUD and interaction with the database can be facilitated with the help of SQLAlchemy for Python. When 5 min read Get column names from CSV using Python CSV (Comma Separated Values) files store tabular data as plain text, with values separated by commas. They are widely used in data analysis, machine learning and statistical modeling. In Python, you can work with CSV files using built-in libraries like csv or higher-level libraries like pandas. In t 2 min read Like