# 📘 Python List Exercises These exercises will help you get really good at using lists in Python. You'll practice things like loops, slicing, grouping, and working with nested lists. --- ## PYEKX-6280ca9a **Flatten a Deeply Nested List** ### Problem Write a function `flatten_list(nested_list)` that flattens an arbitrarily nested list into a single-level list. ### Example ```python flatten_list([1, [2, 3], [4, [5, 6], 7]]) # Output: [1, 2, 3, 4, 5, 6, 7] ``` ### Why? (*Why it matters*) Flattening is common in: - ETL pipelines - JSON parsing - Recursive DOM traversal ### Pro Constraints - Must support arbitrary levels of nesting. - Must not mutate the input list. - Use recursion or a stack-based approach. - Bonus: Implement both recursive and iterative versions. --- ## PYEKX-1d45ec3e **Group Consecutive Duplicates** ### Problem Write a function `group_consecutive(lst)` that groups **only consecutive** duplicate elements into sublists. ### Example ```python group_consecutive([1, 1, 2, 2, 2, 3, 1, 1]) # Output: [[1, 1], [2, 2, 2], [3], [1, 1]] ``` ### Why? This mimics: - Run-length encoding - Log event grouping - Real-time batching by last-seen state ### Pro Constraints - Avoid using third-party libraries. - Maintain original order. - Think about how to handle empty input and non-integer elements. --- ## PYEKX-3c65900f **Rotate List (Bidirectional) k Times** ### Problem Write a function `rotate_list(lst, k)` that rotates the list **right** by `k` steps. > Support negative `k` to rotate left. ### Example ```python rotate_list([1, 2, 3, 4, 5], 2) # Output: [4, 5, 1, 2, 3] rotate_list([1, 2, 3, 4, 5], -2) # Output: [3, 4, 5, 1, 2] ``` ### Why? - Common in **buffer management, scheduling, circular data** structures. - Reinforces modular math and slicing mastery. ### Pro Constraints - Solve it using slicing. - Then try using only loops - no slicing. - Handle large `k` values: `rotate_list([1,2,3], 100)`. --- ## PYEKX-6a7bc172 **List Difference (Ordered & Counted)** ### Problem Write a function `diff_lists(a, b)` that removes one matching element in `b` for each occurrence found in `a`, preserving order. ### Example ```python diff_lists([1, 2, 2, 3, 4], [2, 4]) # Output: [1, 2, 3] ``` ### Why? - Models real-world inventory adjustments. - Used in symmetric difference, record purging, filtering with quantity. ### Pro Constraints - Do not use `collections.Counter`. - Optimize for readability and minimal mutations. - Bonus: Add a flag to toggle whether duplicates in b remove all vs one. --- ## Pro Tips - Try solving without using libraries. - Then try versions using `collections.Counter`, `itertools`, or `functools` for practice. - Write unit tests for edge cases: - Empty lists - Deep nesting - Large `k` values - Negative numbers ```python def test_flatten(): assert flatten_list([1, [2], [[3]]]) == [1, 2, 3] assert flatten_list([]) == [] assert flatten_list([[], [[], [[]]]]) == [] def test_rotate(): assert rotate_list([1,2,3], 0) == [1,2,3] assert rotate_list([1,2,3], 3) == [1,2,3] assert rotate_list([1,2,3], -1) == [2,3,1] ```