By WebNest Studio Engineering Team
Python Tutorial
Introduction to Data Handling in Python
Beyond writing individual scripts, one of Python's most common real-world uses is working with structured data — reading it in, examining it, filtering it, and summarizing it. This is the foundation for everything from simple reporting scripts to full data-analysis pipelines built on libraries like pandas.
Even without any external libraries, Python's own core toolkit — lists of dictionaries, comprehensions, loops, and built-in functions like sum(), len(), and sorted() — is already enough to explore and summarize a small dataset, and understanding this "plain Python" foundation makes it much easier to learn data-focused libraries later.
Why it matters
Rows from a database, lines of a CSV file and JSON from an API all arrive in Python as a list of dictionaries. Filtering, totalling, sorting and finding the top record in that shape are the bread and butter of scripts and back-end code, and doing them well in plain Python is the foundation for moving on to pandas.
Representing Structured Data as a List of Dictionaries
A very common and natural way to represent a small table of records in plain Python is as a list of dictionaries, where each dictionary is one record (like one row) and its keys are the field names (like column headers). This mirrors exactly how data typically arrives from a JSON API or a parsed CSV file, which is why it is a natural starting point before reaching for a dedicated data library.
Iterating and Filtering Records
A plain for loop over a list of dictionaries lets you inspect, filter, or transform each record. Combined with an if condition, this is enough to answer questions like "which records match a certain criterion," and a list comprehension can express the same filtering operation in a single concise line.
Summarizing Data with Built-in Functions
Python's built-in functions sum(), len(), max(), min(), and sorted() cover many basic summary tasks without needing any external library: computing totals and averages, counting records, finding extremes, and ordering data by a chosen field (using the key= argument to sort by a dictionary value, often combined with a lambda expression).
Examples
Filtering and summarizing a list of dictionaries representing sales records
sales = [
{"product": "Widget", "units": 12, "price": 9.5},
{"product": "Gadget", "units": 5, "price": 25.0},
{"product": "Widget", "units": 8, "price": 9.5},
{"product": "Gizmo", "units": 20, "price": 3.25},
]
total_units = sum(item["units"] for item in sales)
print("Total units sold:", total_units)
widget_sales = [item for item in sales if item["product"] == "Widget"]
print("Widget records:", widget_sales)
total_revenue = sum(item["units"] * item["price"] for item in sales)
print(f"Total revenue: {total_revenue:.2f}")
Total units sold: 45
Widget records: [{'product': 'Widget', 'units': 12, 'price': 9.5}, {'product': 'Widget', 'units': 8, 'price': 9.5}]
Total revenue: 380.00
Sorting records by a field and finding the top result
sales = [
{"product": "Widget", "units": 12},
{"product": "Gadget", "units": 5},
{"product": "Gizmo", "units": 20},
]
sorted_by_units = sorted(sales, key=lambda item: item["units"], reverse=True)
for record in sorted_by_units:
print(record["product"], record["units"])
best_seller = max(sales, key=lambda item: item["units"])
print("Best seller:", best_seller["product"])
Gizmo 20
Widget 12
Gadget 5
Best seller: Gizmo
Common Mistakes
- Trying to sum or average a field without first checking that every record actually contains that key, causing a KeyError on incomplete data.
- Forgetting that sorted() returns a new list and does not sort a list of dictionaries in place, unless list.sort() is used instead.
- Using max()/min() without a key= argument on a list of dictionaries, which tries to compare the dictionaries themselves and raises a TypeError.
- Writing a manual loop with a running total for something a single sum() or a comprehension already expresses more clearly and safely.
Exercise
From the list of sales records, print the total amount for the north region, the item with the largest amount, and the item names ordered from the largest amount to the smallest.
Expected output: 420, bag, ['bag', 'book', 'pen'] (one per line)
sales = [
{"item": "pen", "region": "north", "amount": 120},
{"item": "bag", "region": "south", "amount": 450},
{"item": "book", "region": "north", "amount": 300},
]
# TODO: print the total amount for the north region
# TODO: print the item with the largest amount
# TODO: print the item names sorted by amount, largest first
Show hints
- A generator expression with a condition can be passed to
sum. maxandsortedboth acceptkey=lambda record: record["amount"].
Show solution
sales = [
{"item": "pen", "region": "north", "amount": 120},
{"item": "bag", "region": "south", "amount": 450},
{"item": "book", "region": "north", "amount": 300},
]
print(sum(record["amount"] for record in sales if record["region"] == "north"))
print(max(sales, key=lambda record: record["amount"])["item"])
ordered = sorted(sales, key=lambda record: record["amount"], reverse=True)
print([record["item"] for record in ordered])
Quick quiz
1. Which structure most naturally represents a table of records in plain Python?
- A. A string
- B. A nested set
- C. A set of tuples
- D. A list of dictionaries
Show answer
D. Each dictionary is a row, and its keys are the column names.
2. How do you find the record with the highest value of one field?
- A. max(records, key=lambda r: r["field"])
- B. max(records)
- C. records.max("field")
- D. sorted(records)[0]
Show answer
A. Dictionaries cannot be compared directly, so a key function says what to compare.
3. What does [r for r in records if r["active"]] produce?
- A. The number of active records
- B. A new list containing only the active records
- C. A dictionary
- D. It modifies records in place
Show answer
B. The original list is unchanged.
Interview questions
How would you group a list of records by one field and total another in plain Python?
Show answer
Use a dictionary keyed by the grouping field. With collections.defaultdict(int), loop over the records and add each amount to totals[record["region"]]. The result maps each group to its total. The same pattern with defaultdict(list) collects the records of each group. pandas does this with groupby for larger data.
When should you move from lists of dictionaries to pandas?
Show answer
When the data grows large or the analysis involves grouping, joining tables, handling missing values or working with dates. pandas performs these operations on whole columns in optimised code, and expresses them in a line where plain Python needs loops. For small data and simple filters, plain Python is perfectly adequate and has no dependencies.
Key Points to Remember
- A list of dictionaries is a natural, dependency-free way to represent tabular/structured data in Python.
- Plain for loops and list comprehensions are enough to filter and transform records before reaching for external libraries.
- sum(), len(), max(), min(), and sorted() (often paired with a key=lambda) cover most basic data-summary tasks.
- This plain-Python approach is the on-ramp to dedicated data libraries like pandas, which automate the same ideas at scale.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering