0
0
Pandasdata~5 mins

Creating DataFrame from list of dictionaries in Pandas - Performance & Efficiency

Choose your learning style9 modes available
Time Complexity: Creating DataFrame from list of dictionaries
O(n)
Understanding Time Complexity

We want to understand how the time needed to create a DataFrame changes as the input list grows.

How does the work increase when we add more dictionaries to the list?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

import pandas as pd

list_of_dicts = [
    {'name': 'Alice', 'age': 25},
    {'name': 'Bob', 'age': 30},
    {'name': 'Charlie', 'age': 35}
]

df = pd.DataFrame(list_of_dicts)

This code creates a DataFrame from a list where each item is a dictionary representing a row.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: pandas reads each dictionary in the list to build rows.
  • How many times: Once for each dictionary in the list (n times).
How Execution Grows With Input

As the list grows, pandas processes each dictionary one by one to form the DataFrame.

Input Size (n)Approx. Operations
10About 10 dictionary reads
100About 100 dictionary reads
1000About 1000 dictionary reads

Pattern observation: The work grows directly with the number of dictionaries; doubling the list roughly doubles the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to create the DataFrame grows linearly with the number of dictionaries in the list.

Common Mistake

[X] Wrong: "Creating a DataFrame from a list of dictionaries is instant no matter the size."

[OK] Correct: Each dictionary must be read and processed, so more dictionaries mean more work and more time.

Interview Connect

Understanding how data size affects DataFrame creation helps you reason about performance in real projects and interviews.

Self-Check

"What if we changed the input from a list of dictionaries to a dictionary of lists? How would the time complexity change?"