Intermediate Python Training -
Module 2
Data Structures: Lists, Tuples,
Dictionaries, Sets
Introduction to Data Structures
• What are data structures?
• Classified as linear and non-linear structures
• Used to organize and store data efficiently
Lists - Basics
• Ordered, mutable collections of items
• Created using square brackets: my_list = [1, 2,
3]
• Supports indexing and slicing
Lists - Common Methods
• append(item): Adds item to end
• insert(index, item): Inserts item at given index
• remove(item): Removes first matching item
• pop(index): Removes item at index (default:
last)
• clear(): Removes all items
Lists - Advanced Methods
• extend(iterable): Adds elements from iterable
• sort(): Sorts list in-place
• reverse(): Reverses list in-place
• List comprehensions: [x*2 for x in range(5)]
List Use Case - To-do List Example
• Managing tasks using a list
• Add, remove, and list tasks dynamically
• tasks = ['email', 'meeting']
• tasks.append('code review')
Tuples - Basics
• Ordered, immutable collections
• Created using parentheses: my_tuple = (1, 2,
3)
• Supports indexing but not item assignment
Tuples - Use Cases
• Good for fixed data: coordinates, RGB values
• Faster than lists for read-only data
Dictionaries - Basics
• Key-value pairs for fast lookup
• Created with curly braces: {'name': 'Alice'}
• Access with keys: person['name']
Dictionaries - Common Methods
• get(key, default): Safe access
• update(dict): Merge data
• pop(key): Remove key and return value
• keys(), values(), items() for iteration
Dictionary Use Case - Student
Records
• student = {'name': 'Alice', 'score': 90}
• Add/update fields with student['grade'] = 'A'
• Remove fields using pop()
Sets - Basics
• Unordered collection of unique items
• Created using: my_set = {1, 2, 3}
• No duplicate elements allowed
Sets - Operations
• add(item), remove(item)
• Set algebra: union (|), intersection (&),
difference (-)
• Efficient membership tests: x in my_set
List vs Tuple vs Dict vs Set
• List: ordered, mutable, allows duplicates
• Tuple: ordered, immutable, allows duplicates
• Dict: key-value, mutable, fast lookup
• Set: unordered, mutable, no duplicates
Practice Exercises
• Create a to-do list using list methods
• Store and update student data in a dictionary
• Use sets to find unique items from a list
• Try tuple unpacking: x, y = (1, 2)
Summary & Best Practices
• Choose the right data structure for the task
• Use tuples for fixed data, dicts for mappings
• Practice accessing, modifying, and iterating
• Avoid modifying collections while iterating