Python / Core Python Fundamentals Interview Questions
How do you work with nested data structures such as a list of dictionaries?
A list of dictionaries is the most common Python pattern for representing structured data from APIs, CSV rows, database query results, and JSON payloads. Each dictionary is one record; the list is the collection.
employees = [
{'name': 'Alice', 'dept': 'Engineering', 'salary': 95000},
{'name': 'Bob', 'dept': 'Marketing', 'salary': 72000},
{'name': 'Carol', 'dept': 'Engineering', 'salary': 105000},
{'name': 'Dave', 'dept': 'Marketing', 'salary': 68000},
]
# Filter: Engineering employees
eng = [e for e in employees if e['dept'] == 'Engineering']
# Map: extract names only
names = [e['name'] for e in employees]
# Sort by salary descending
ranked = sorted(employees, key=lambda e: e['salary'], reverse=True)
# Group by department using defaultdict
from collections import defaultdict
by_dept = defaultdict(list)
for emp in employees:
by_dept[emp['dept']].append(emp['name'])
# {'Engineering': ['Alice', 'Carol'], 'Marketing': ['Bob', 'Dave']}
# Average salary per department
dept_salary = {}
for dept, members in by_dept.items():
salaries = [e['salary'] for e in employees if e['name'] in members]
dept_salary[dept] = sum(salaries) / len(salaries)When accessing nested values that may not exist, chain .get() calls or use a library like glom for deeply nested paths. Safe access pattern: record.get('address', {}).get('city', 'Unknown').
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
