๐ Day 04 : Lists
๐ฏ Enterprise Objective
Lists are Python's most versatile data structure and the foundation for data manipulation. Today we master list creation, modification, comprehensions, and advanced patterns. You will learn the professional techniques that translate directly to pandas DataFrame operations.
๐ Strategic Overview
| # | Topic | Key Concept | Core Use Case |
|---|---|---|---|
| 1 | Creation | Literal, range(), repetition | Building datasets |
| 2 | Indexing & Slicing | lst[start:stop:step] | Data extraction |
| 3 | Methods | .append(), .sort(), .pop() | In-place modification |
| 4 | Copying | Shallow vs deep copy | Data integrity |
| 5 | Comprehensions | [expr for x in iter if cond] | Transformation |
| 6 | Built-in Functions | enumerate, zip, sorted | Aggregation |
| 7 | Nested Lists | 2D data, matrices | Tabular data |
| 8 | Stack & Queue | LIFO/FIFO patterns | Algorithms |
1. List Creation & Basics : Dynamic Mutable Sequences
A list is an ordered, mutable collection that can hold items of any type. Lists are the most versatile data structure in Python โ they grow and shrink dynamically, support indexing/slicing, and are the backbone of data pipelines.
| Creation Method | Example | Use Case |
|---|---|---|
| Literal | [1, 2, 3] | Quick, explicit |
list() constructor | list('abc') โ ['a','b','c'] | Convert iterables |
list(range(n)) | list(range(5)) โ [0,1,2,3,4] | Numeric sequences |
| Repetition | [0] * 5 โ [0,0,0,0,0] | Initialize with defaults |
| Comprehension | [x**2 for x in range(5)] | Functional creation |
๐ผ Why Data Analysts Care
โข Data buffers: Lists hold rows of data during ETL processing
โข Feature collections: Gather column names, metrics, or results dynamically
โข Pipeline stages: Accumulate transformed records before writing to output
โ ๏ธ Shallow Copy Trap
matrix = [[0]*3]*3 creates 3 references to the same inner list. Modifying one row changes all rows. Use [[0]*3 for _ in range(3)] instead.
๐ง Pro Tip
Use list() to convert any iterable: list('hello') โ ['h','e','l','l','o'], list(range(5)) โ [0,1,2,3,4].
๐งช Concept Checks: List Creation
Q1. Create a list of the first 10 even numbers using list(range(...)). Print the list and its length.
Q2. Create a list containing: an int, a float, a string, a boolean, and None. Print each element with its type using a loop.
Q3. Demonstrate the shallow copy trap: create matrix = [[0]3]3, modify matrix[0][0] = 1, then print the full matrix. Explain what happened.
Q4. Create the same 3x3 matrix correctly using a list comprehension [[0]*3 for _ in range(3)]. Modify [0][0] and verify only one row changed.
Q5. Convert the string "Data Analytics" into a list of characters, then into a list of words. Print both results.
2. List Indexing & Slicing : Precision Data Access
Lists use the same indexing and slicing syntax as strings: lst[i] for single access, lst[start:stop:step] for slices. Unlike strings, list slicing returns a new list, and you can assign to slices to modify multiple elements at once.
| Operation | Syntax | Description |
|---|---|---|
| Access | lst[0], lst[-1] | First, last element |
| Slice | lst[1:4] | Elements at index 1,2,3 |
| Step | lst[::2] | Every other element |
| Reverse | lst[::-1] | Reversed copy |
| Slice assign | lst[1:3] = [10, 20] | Replace range |
๐ผ Why Data Analysts Care
โข Batch selection: Select specific rows/columns from data matrices
โข Pagination: data[offset:offset+limit] โ paginate query results
โข Window operations: data[i:i+window_size] โ sliding window analysis
๐ง Pro Tip
Slice assignment can change list length: lst[1:3] = [10, 20, 30, 40] replaces 2 elements with 4.
๐งช Concept Checks: Indexing & Slicing
Q1. Given nums = list(range(1, 21)), extract: first 5, last 5, every 3rd element, and reversed list. Print each.
Q2. Use slice assignment to replace elements at index 2-4 in data = [1,2,3,4,5] with [30,40,50]. Print the result.
Q3. Write code to rotate a list left by 2 positions: [1,2,3,4,5] โ [3,4,5,1,2]. Use only slicing.
Q4. Given matrix = [[1,2,3],[4,5,6],[7,8,9]], extract the second column [2,5,8] using indexing in a loop.
Q5. Use negative slicing to get the second-to-last element and the last 3 elements of data = [10,20,30,40,50,60]. Print both.
3. List Methods : In-Place Modification
Lists have powerful methods for adding, removing, and reordering elements. Most methods modify the list in-place and return None. This is a common gotcha โ never write lst = lst.sort().
| Method | Action | Returns |
|---|---|---|
.append(x) | Add to end | None |
.extend(iter) | Add multiple items | None |
.insert(i, x) | Insert at position | None |
.remove(x) | Remove first occurrence | None |
.pop(i) | Remove and return at index | The removed item |
.sort() | Sort in-place | None |
.reverse() | Reverse in-place | None |
.index(x) | Find position | Index int |
.count(x) | Count occurrences | Count int |
๐ผ Why Data Analysts Care
โข Data accumulation: results.append(row) โ building datasets row by row
โข Deduplication: .count() and .remove() for cleaning duplicates
โข Sorting: data.sort(key=lambda x: x['date']) โ sort records by field
โ ๏ธ sort() Returns None
lst.sort() modifies in-place and returns None. Writing lst = lst.sort() destroys your list! Use sorted(lst) if you need a new sorted list.
๐งช Concept Checks: List Methods
Q1. Start with lst = [3, 1, 4, 1, 5]. Use .append(), .extend(), .insert() to add elements. Print the list after each operation.
Q2. Given data = [5, 3, 8, 1, 9, 2], sort it in ascending order, then reverse it. Use both in-place methods. Print after each step.
Q3. Demonstrate the sort() trap: show that result = [3,1,2].sort() gives None. Then show the correct way using sorted().
Q4. Given items = ["apple", "banana", "apple", "cherry", "apple"], count occurrences of "apple" and find its first index. Print both.
Q5. Write code that removes all occurrences of a value from a list (not just the first). Remove all 0s from [1, 0, 2, 0, 3, 0].
4. Copying Lists : Shallow vs Deep Copy
Assignment (b = a) creates a reference, not a copy. .copy() or list(a) creates a shallow copy. For nested lists, you need copy.deepcopy() to get a fully independent copy.
| Method | Creates | Nested Objects |
|---|---|---|
b = a | Reference (alias) | Shared โ changes affect both |
b = a.copy() | Shallow copy | Still shared (inner refs) |
b = a[:] | Shallow copy | Still shared (inner refs) |
b = copy.deepcopy(a) | Deep copy | Fully independent |
๐ผ Why Data Analysts Care
โข Data integrity: Always copy before modifying to preserve original data
โข Function safety: Copy inputs inside functions to avoid side effects
โข Snapshot patterns: Take deep copies as checkpoints in data transformations
โ ๏ธ Shallow Copy Surprise
With nested lists, .copy() only copies the outer list. Inner lists are still shared references. Modifying an inner list in the copy changes the original too.
๐งช Concept Checks: Copying
Q1. Create a = [1, 2, 3] and b = a. Modify b.append(4). Print both. Explain why a changed too.
Q2. Fix the above by using .copy(). Show that modifying b no longer affects a.
Q3. Create nested = [[1, 2], [3, 4]]. Make a shallow copy. Modify copy[0][0] = 99. Show that the original also changed.
Q4. Fix the nested copy issue using copy.deepcopy(). Verify the original is unaffected by changes to the deep copy.
Q5. Write a function safe_process(data) that takes a list, makes a deep copy internally, sorts the copy, and returns it โ leaving the original unchanged. Test it.
5. List Comprehensions : Functional Data Transformation
A list comprehension is a concise way to create lists: [expression for item in iterable if condition]. They replace multi-line loops with a single readable line and are faster than equivalent for-loops.
# Loop version (4 lines)
result = []
for x in range(10):
if x % 2 == 0:
result.append(x ** 2)
# Comprehension (1 line)
result = [x**2 for x in range(10) if x % 2 == 0]
๐ผ Why Data Analysts Care
โข Data filtering: clean = [x for x in data if x is not None]
โข Type conversion: nums = [int(x) for x in string_list]
โข Feature engineering: features = [row['age'] * row['income'] for row in records]
โ ๏ธ Readability Limit
If a comprehension needs more than one condition or nested loops, switch to a regular for-loop. Readability always wins over cleverness.
๐งช Concept Checks: Comprehensions
Q1. Create a list of squares of numbers 1-10 using a comprehension. Print it.
Q2. Given words = ["hello", "world", "python", "data"], create a list of their lengths using a comprehension. Print it.
Q3. Filter: from nums = [1, -2, 3, -4, 5, -6], extract only positive numbers using a comprehension.
Q4. Flatten matrix = [[1,2],[3,4],[5,6]] into [1,2,3,4,5,6] using a nested comprehension.
Q5. Given data = ["42", "hello", "3.14", "7", "abc"], use a comprehension with .isdigit() to extract and convert valid integers. Print the result.
6. Built-in Functions for Lists : Aggregation & Analysis
Python provides powerful built-in functions that work on lists: len(), sum(), min(), max(), sorted(), enumerate(), zip(), map(), filter(), any(), all().
| Function | Purpose | Example |
|---|---|---|
len(lst) | Count elements | len([1,2,3]) โ 3 |
sum(lst) | Total | sum([1,2,3]) โ 6 |
min/max | Extremes | max([3,1,2]) โ 3 |
sorted() | New sorted list | sorted([3,1,2]) โ [1,2,3] |
enumerate() | Index + value | Indexed iteration |
zip() | Pair elements | Parallel iteration |
any()/all() | Boolean checks | Data validation |
๐ผ Why Data Analysts Care
โข Statistics: sum(data)/len(data) โ quick mean calculation
โข Validation: all(x > 0 for x in data) โ check all values positive
โข Paired data: zip(names, scores) โ combine parallel lists
๐ง Pro Tip
enumerate() is always preferred over range(len(lst)): for i, val in enumerate(data): is cleaner and more Pythonic.
๐งช Concept Checks: Built-in Functions
Q1. Given data = [45, 82, 67, 91, 53], compute and print: length, sum, average, min, max.
Q2. Use enumerate() to print each element of fruits = ["apple", "banana", "cherry"] with its 1-based index.
Q3. Use zip() to combine names = ["Alice", "Bob"] and ages = [25, 30] into a list of tuples. Print it.
Q4. Use any() to check if any number in nums = [2, 4, 6, 7, 8] is odd. Use all() to check if all are positive. Print both.
Q5. Use sorted() with a key parameter to sort words = ["banana", "apple", "cherry"] by length. Print the result.
7. Nested Lists : 2D Data Structures
A nested list (list of lists) represents 2D data โ tables, matrices, grids. Access elements with matrix[row][col]. This is the foundation for understanding DataFrames in pandas.
matrix = [
[1, 2, 3], # row 0
[4, 5, 6], # row 1
[7, 8, 9], # row 2
]
matrix[1][2] # row 1, col 2 โ 6
๐ผ Why Data Analysts Care
โข Tabular data: CSV data is naturally a list of lists before converting to DataFrame
โข Matrix operations: Row/column extraction, transposition, aggregation
โข Grid problems: Game boards, pixel maps, adjacency matrices
๐ง Pro Tip
Transpose a matrix with list(zip(*matrix)). This unpacks rows and re-zips them as columns.
๐งช Concept Checks: Nested Lists
Q1. Create a 3x3 matrix using a nested list. Print it row by row in a formatted grid.
Q2. Given matrix = [[1,2,3],[4,5,6],[7,8,9]], extract the diagonal [1,5,9] using indexing.
Q3. Write code to compute the sum of each row and each column in a 3x3 matrix. Print the results.
Q4. Transpose matrix = [[1,2,3],[4,5,6]] into [[1,4],[2,5],[3,6]] using zip(*matrix). Print the result.
Q5. Given a list of student records [["Alice",85],["Bob",92],["Charlie",78]], sort by score (descending) and print the ranked list.
8. Lists as Stacks & Queues : Data Structure Patterns
Lists can function as stacks (LIFO โ Last In, First Out) using append()/pop(), and as queues (FIFO โ First In, First Out) using append()/pop(0). For production queues, use collections.deque for O(1) performance.
| Pattern | Push | Pop | Order |
|---|---|---|---|
| Stack (LIFO) | .append(x) | .pop() | Last in, first out |
| Queue (FIFO) | .append(x) | .pop(0) | First in, first out |
| Deque (fast) | .append(x) | .popleft() | O(1) both ends |
๐ผ Why Data Analysts Care
โข Undo systems: Stack-based undo/redo for data transformations
โข Task queues: FIFO queues for processing jobs in order
โข BFS/DFS: Graph traversal algorithms use stacks and queues
โ ๏ธ list.pop(0) is O(n)
Removing from the front of a list shifts all elements. For frequent front-removal, use collections.deque which is O(1).
๐งช Concept Checks: Stack & Queue
Q1. Implement a stack: push [10, 20, 30], then pop twice. Print the popped values and remaining stack.
Q2. Implement a queue using collections.deque: enqueue ["A", "B", "C"], then dequeue twice. Print results.
Q3. Use a stack to reverse a string: push each character, then pop all. "hello" โ "olleh". Print the result.
Q4. Write a function is_balanced(expr) that uses a stack to check if parentheses are balanced. Test with "(())" and "(()".
Q5. Compare performance: time list.pop(0) vs deque.popleft() for 10000 operations. Print the timing comparison.
๐ ๏ธ Professional Practice Tasks
Theory is useless without muscle memory. Complete these tasks to solidify your understanding.
Task 1 (Data Pipeline): Write a function process_scores(raw) that takes raw = ['85', 'N/A', '92', '', '78', 'absent', '95'], filters out non-numeric entries, converts to int, sorts descending, and returns the top 3. Print the result.
Task 2 (Matrix Calculator): Write functions to add and multiply two 3x3 matrices (list of lists). Test with sample matrices and print formatted results.
Task 3 (Inventory Manager): Create a list of dicts [{'item': 'Laptop', 'qty': 5, 'price': 999}, ...] with 5 products. Write code to: find most expensive, compute total inventory value, sort by price.
Task 4 (Deduplicator): Write a function deduplicate(lst) that removes duplicates while preserving original order. [3,1,4,1,5,3,2] โ [3,1,4,5,2]. Do NOT use set().
Task 5 (Sliding Window): Write a function moving_average(data, window) that computes moving averages. data=[1,3,5,7,9], window=3 โ [3.0, 5.0, 7.0]. Print results.
๐ป Pure Coding Interview Questions
Q1.
Write a function two_sum(nums, target) returning indices of two numbers that add to target. [2,7,11,15], 9 โ [0,1].
Q2.
Write a function rotate_list(lst, k) that rotates right by k positions. [1,2,3,4,5], k=2 โ [4,5,1,2,3].
Q3.
Write a function merge_sorted(a, b) that merges two sorted lists into one sorted list without using sort().
Q4.
Write a function flatten(nested) that flattens arbitrarily nested lists: [1,[2,[3,[4]]],5] โ [1,2,3,4,5]. Use recursion.
Q5.
Write a function find_missing(nums) that finds the missing number in [0,1,2,4,5] โ 3. List contains n-1 numbers from 0 to n.
Q6.
Write a function remove_duplicates_sorted(lst) in-place for a sorted list. [1,1,2,2,3] โ [1,2,3]. Return new length.
Q7.
Write a function max_subarray_sum(nums) implementing Kadane's algorithm. Find contiguous subarray with largest sum.
Q8.
Write a function intersection(a, b) returning common elements preserving order from a. Handle duplicates correctly.
Q9.
Write a function chunk(lst, size) splitting list into chunks: [1,2,3,4,5], size=2 โ [[1,2],[3,4],[5]].
Q10.
Write a function spiral_order(matrix) returning elements in spiral order from a 2D matrix.
Q11.
Write a function product_except_self(nums) returning array where each element is product of all others. No division.
Q12.
Write a function longest_consecutive(nums) finding longest consecutive sequence. [100,4,200,1,3,2] โ 4 (sequence 1-4).
Q13.
Write a function group_by(lst, key_func) that groups elements by a key function. Return a dict of lists.
Q14.
Write a function interleave(a, b) merging alternately: [1,3,5],[2,4,6] โ [1,2,3,4,5,6]. Handle different lengths.
Q15.
Write a function partition(lst, pred) splitting into two lists based on predicate. Return (true_list, false_list).
Q16.
Write a function find_duplicates(lst) returning all elements that appear more than once. Preserve first-seen order.
Q17.
Write code implementing binary search on a sorted list. Return index or -1. No bisect module.
Q18.
Write a function matrix_multiply(A, B) for matrix multiplication. Validate dimensions. Return result matrix.
Q19.
Write a function compress(lst) implementing RLE: [1,1,2,2,2,3] โ [(1,2),(2,3),(3,1)].
Q20.
Write a function kth_largest(lst, k) finding kth largest element without full sort. Use partial sort or heap.
Q21.
Write a function is_sorted(lst) checking if list is sorted (ascending or descending). Handle equal elements.
Q22.
Write a function dutch_flag(lst) sorting [0,1,2,0,1,2] in-place with single pass. Three-way partition.
Q23.
Write a function subsets(lst) generating all subsets (power set). [1,2] โ [[], [1], [2], [1,2]].
Q24.
Write a function next_greater(lst) finding next greater element for each: [4,5,2,10] โ [5,10,10,-1].
Q25.
Write a function max_profit(prices) finding max stock profit from single buy-sell. [7,1,5,3,6,4] โ 5.
๐ Day 4 Executive Summary
| # | Topic | Key Takeaway | Professional Application |
|---|---|---|---|
| 1 | Creation | Mutable, dynamic, any type | Data buffers, accumulators |
| 2 | Indexing | Zero-based; slice assignment modifies | Data extraction, pagination |
| 3 | Methods | In-place methods return None | Data manipulation |
| 4 | Copying | .copy() is shallow; use deepcopy for nested | Data integrity |
| 5 | Comprehensions | [expr for x in iter if cond] | Filtering, transformation |
| 6 | Built-ins | enumerate, zip, any, all | Aggregation, pairing |
| 7 | Nested Lists | 2D data; zip(*m) transposes | Tabular data, matrices |
| 8 | Stack/Queue | LIFO/FIFO patterns; use deque | Algorithms, task processing |
โ Instructor's End-of-Day Checklist
โข [ ] I understand mutability and can avoid accidental aliasing.
โข [ ] I know the difference between .sort() (in-place) and sorted() (new list).
โข [ ] I can use list comprehensions for filtering and transformation.
โข [ ] I understand shallow vs deep copy for nested structures.
โข [ ] I can use enumerate() and zip() for Pythonic iteration.
โข [ ] I have completed all 5 practice tasks.
โข [ ] I have reviewed all 25 interview questions.