flatten nested list

Create a Python function `flatten_nested_list(nested_list)` that takes a list of lists (which may contain other nested lists) and returns a single flattened list containing all the elements in a depth-first order.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

Example:

python

Example input:

nested_list = [1, [2, [3, 4], 5], 6, [7, 8, [9, [10]]]]

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

Expected output:

flattened_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Constraints:

1. The input list can contain integers, other lists, or both.

2. The function should handle arbitrarily deep nesting.

3. You are not allowed to use Python’s built-in `itertools.chain` or similar built-in functions for flattening the list.

Test Cases:

python

Test Case 1:

assert flatten_nested_list([1, 2, [3, 4], [5, [6, 7]]]) == [1, 2, 3, 4, 5, 6, 7]

Test Case 2:

assert flatten_nested_list([[], [1], [[2, 3]], [4, [5, []]]]) == [1, 2, 3, 4, 5]

Test Case 3:

assert flatten_nested_list([[[[1, 2], 3], 4], 5, [], [6, [7, [8, [9]]]]]) == [1, 2, 3, 4, 5, 6, 7, 8, 9]

Write the `flatten_nested_list` function that passes all the above test cases.

Are you stuck with your online class?
Get help from our team of writers!

Order your essay today and save 20% with the discount code RAPID