How to Find All Subsets of a Given List Using Recursion in Python etd_admin, September 10, 2025September 10, 2025 Working with subsets is a common problem in computer science, especially in topics like combinatorics, backtracking, and dynamic programming. A subset is simply any selection of elements from a list, including the empty set and the set itself. In this article, we’ll break down how to find all subsets of a given list using recursion in Python. We’ll keep the explanation straightforward and provide code samples so you can follow along. Understanding the Problem Let’s say we have a list like this: nums = [1, 2, 3]nums = [1, 2, 3] The subsets of this list are: [], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3][], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3] Notice that every element can either be included or excluded in a subset. This is exactly why recursion is a perfect solution—it can “branch” at each step into two choices: take it or leave it. Recursive Approach Explained The recursive idea works like this: Start with an empty list as your current subset. At each step, decide whether to include the current element. Recurse until you’ve considered every element in the list. Collect the subsets along the way. This way, you explore all possibilities. Python Code Example Here’s a clean implementation: def find_subsets(nums): result = [] def backtrack(index, current): # If we've considered all elements, add the current subset if index == len(nums): result.append(current[:]) # make a copy return # Decision 1: Exclude the current number backtrack(index + 1, current) # Decision 2: Include the current number current.append(nums[index]) backtrack(index + 1, current) # Backtrack: remove the last element current.pop() backtrack(0, []) return result # Example usage nums = [1, 2, 3] print(find_subsets(nums))def find_subsets(nums): result = [] def backtrack(index, current): # If we've considered all elements, add the current subset if index == len(nums): result.append(current[:]) # make a copy return # Decision 1: Exclude the current number backtrack(index + 1, current) # Decision 2: Include the current number current.append(nums[index]) backtrack(index + 1, current) # Backtrack: remove the last element current.pop() backtrack(0, []) return result # Example usage nums = [1, 2, 3] print(find_subsets(nums)) Output: [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]][[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]] Why This Works Each recursive call handles one element. By branching into include or exclude, you naturally cover all possible combinations. The base case (when index == len(nums)) ensures the subset is recorded once you’ve reached the end. Learning how to find all subsets of a given list using recursion in Python is an excellent step toward mastering recursion and backtracking. Not only does it teach you problem-solving techniques, but it also prepares you for tackling advanced algorithms. Try experimenting with different lists and see how the subsets change. Once you’re comfortable, you’ll realize this same approach can be extended to solve problems like combinations, permutations, and even solving puzzles. Python PythonRecursion