Python Data Structures: Practice Problems
Here's a comprehensive collection of practice problems covering lists, dictionaries, sets, and
tuples with their methods. Problems are organized by difficulty level and data structure type.
Lists Practice Problems
Beginner Level
Problem 1: Basic List Operations
# Given list: numbers = [10, 20, 30, 40, 50]
# 1. Add 60 to the end of the list
# 2. Insert 5 at the beginning
# 3. Remove the number 30
# 4. Find the index of 40
# 5. Count how many times 20 appears
Problem 2: Shopping Cart
Create a shopping cart program that:
Starts with an empty list
Allows users to add items using append()
Removes items using remove()
Shows total items using len()
Clears the entire cart using clear()
Problem 3: List Sorting
# Given: grades = [85, 92, 78, 96, 88, 76, 89, 94]
# 1. Sort the grades in ascending order
# 2. Sort in descending order (use reverse parameter)
# 3. Find the highest and lowest grades
# 4. Create a copy of the list before sorting
Intermediate Level
Problem 4: Student Grade Manager
Create a program that manages student grades:
Use extend() to add multiple grades at once
Use insert() to add a grade at a specific position
Use pop() to remove and return the last grade
Use index() to find position of a specific grade
Handle the case when a grade doesn't exist
Problem 5: List Manipulation Challenge
# Given: data = [1, 2, 3, 2, 4, 2, 5, 6, 2]
# 1. Count occurrences of number 2
# 2. Remove only the first occurrence of 2
# 3. Remove all occurrences of 2 using a loop
# 4. Reverse the final list
# 5. Create a sorted copy without modifying original
Advanced Level
Problem 6: Playlist Manager
Build a music playlist manager using all list methods:
Add songs to end and specific positions
Remove songs by name and position
Shuffle playlist (use reverse() and custom logic)
Handle duplicates and missing songs
Create backup copies and merge playlists
Dictionaries Practice Problems
Beginner Level
Problem 7: Student Information
# Create a student dictionary with name, age, grade, subjects
# 1. Use get() to safely access student's city (default: "Unknown")
# 2. Add a new key-value pair for "email"
# 3. Update the age using update()
# 4. Print all keys, values, and items
Problem 8: Word Counter
Create a program that counts word frequency in a sentence:
Use setdefault() to initialize counts
Use get() to retrieve current counts
Display results using items()
Problem 9: Phone Directory
# contacts = {"Alice": "123-456", "Bob": "789-012"}
# 1. Add new contacts using update()
# 2. Remove Alice using pop()
# 3. Get Charlie's number (default: "Not found")
# 4. Clear all contacts
# 5. Create directory from list of names with empty numbers
Intermediate Level
Problem 10: Inventory Management
Build an inventory system:
Use setdefault() to add new items with default quantities
Use pop() to remove items and return their quantities
Use popitem() to remove the most recently added item
Merge inventories from different stores using update()
Problem 11: Grade Book
# Create a gradebook where keys are student names, values are grade lists
# 1. Use setdefault() to add new students
# 2. Calculate average grades for each student
# 3. Find students with grades above a threshold
# 4. Create a copy for backup before modifications
Advanced Level
Problem 12: Data Analysis Dashboard
Create a program that analyzes survey data:
Use fromkeys() to initialize response categories
Process multiple data sources with update()
Use dictionary comprehensions with .items()
Handle missing data gracefully with get()
Sets Practice Problems
Beginner Level
Problem 13: Duplicate Remover
# Given: numbers = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7]
# 1. Convert to set to remove duplicates
# 2. Add number 8 to the set
# 3. Remove number 3 using discard()
# 4. Try to remove number 10 using both remove() and discard()
Problem 14: Membership Testing
Create a program that:
Maintains a set of valid user IDs
Checks if a user ID exists in the set
Adds new IDs and removes inactive ones
Uses isdisjoint() to check for conflicts
Problem 15: Set Operations Basics
# set1 = {1, 2, 3, 4, 5}
# set2 = {4, 5, 6, 7, 8}
# 1. Find union using both union() and | operator
# 2. Find intersection using both methods
# 3. Find difference (set1 - set2)
# 4. Find symmetric difference
Intermediate Level
Problem 16: Course Enrollment System
Build a system that tracks student course enrollments:
Use intersection() to find common courses between students
Use difference() to find courses only one student takes
Use symmetric_difference() to find exclusive courses
Use issubset() and issuperset() to compare course loads
Problem 17: Set Comparisons
# Create three sets representing skills of different employees
# 1. Find employees with overlapping skills
# 2. Identify unique skills per employee
# 3. Find the most versatile employee (superset of others)
# 4. Check if any two employees have completely different skills
Advanced Level
Problem 18: Data Validation System
Create a validation system using sets:
Maintain sets of valid email domains, user roles, and permissions
Use set operations to validate user data
Implement privilege checking using subset operations
Handle multiple validation rules efficiently
Tuples Practice Problems
Beginner Level
Problem 19: Coordinate System
# point = (3, 4, 5) # x, y, z coordinates
# 1. Find how many times coordinate 4 appears
# 2. Find the index of coordinate 5
# 3. Calculate distance from origin using tuple values
# 4. Create a new tuple with doubled coordinates
Problem 20: Student Records
# student = ("Alice", 20, "Computer Science", 3.8)
# 1. Extract name and GPA using indexing
# 2. Count occurrences of any specific value
# 3. Find index of the major
# 4. Convert to list, modify, and convert back to tuple
Intermediate Level
Problem 21: Data Processing
# data = ((1, "A"), (2, "B"), (3, "C"), (2, "D"), (1, "E"))
# 1. Count how many tuples contain the number 2
# 2. Find indices of all tuples containing "A"
# 3. Extract all first elements into a new tuple
# 4. Sort the tuple of tuples by first element
Problem 22: Time Series Data
Create a program that works with time-based data stored as tuples:
Store (timestamp, value) pairs
Count occurrences of specific timestamps
Find positions of specific values
Process the data while maintaining immutability
Advanced Level
Problem 23: Configuration Manager
Use tuples to store immutable configuration data:
Create nested tuples for complex configurations
Implement search functionality using index() and count()
Convert between tuples and other data structures
Handle configuration validation
Mixed Data Structures Problems
Problem 24: Social Media Analytics
Combine all data structures:
Lists: Store posts chronologically
Dictionaries: Map users to their follower counts
Sets: Track unique hashtags and user interactions
Tuples: Store immutable post metadata (timestamp, author, type)
Problem 25: Game Inventory System
Create a complex game inventory:
Lists: Manage player inventory with quantities
Dictionaries: Map item names to properties
Sets: Track unique items owned and achievements unlocked
Tuples: Store item stats (damage, defense, rarity)
Problem 26: Library Management System
Build a comprehensive library system:
Lists: Maintain reading lists and book queues
Dictionaries: Map ISBN to book details, users to borrowed books
Sets: Track available genres, overdue borrowers
Tuples: Store immutable book information (title, author, year)
Challenge Problems
Problem 27: Data Structure Performance Test
Compare performance of different operations:
Membership testing: list vs set vs dictionary
Adding elements: list.append() vs set.add()
Removing elements: different methods across structures
Memory usage comparison
Problem 28: Advanced Data Manipulation
Create a program that:
Converts between different data structures efficiently
Handles nested data structures (lists of dictionaries, sets of tuples)
Implements custom sorting and filtering
Manages data persistence and backup
Problem 29: Real-World Application
Build a complete application (choose one):
E-commerce: Shopping cart, inventory, user preferences
School System: Students, courses, grades, schedules
Social Network: Users, posts, connections, messages
Financial Tracker: Accounts, transactions, categories, budgets
Each application should utilize all four data structures and their methods appropriately based on
the data requirements and access patterns.
Tips for Solving These Problems
1. Start Simple: Begin with basic method usage before combining operations
2. Test Edge Cases: Empty structures, missing elements, duplicate values
3. Performance Considerations: Choose the right data structure for the task
4. Error Handling: Use methods like get() and discard() to avoid exceptions
5. Documentation: Comment your code explaining why you chose specific methods
These problems progress from basic method usage to complex real-world applications, giving
you hands-on experience with all Python data structures and their methods.