Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

As you know, lists in Python can contain objects of any kind. It is possible, th

ID: 3741302 • Letter: A

Question

As you know, lists in Python can contain objects of any kind. It is possible, therefore, to have a list which contains another list -a nested list. You may even have a list like where the elements are nested multiple times, and in no sensible order. Your assignment is to write a recursive function, flattenList(alist), that returns a “flattened" version of any given list. That is, all elements (not including lists) of the given list should be in the final, non-nested list in their original order. For example, if the above list was the input, your function should return Your program does not need a main. You do not need to prompt for input from the user; just write the function, and all testing on our part will be done by calling your function with parameters we have defined. Of course, you may test your program however you like.

Explanation / Answer

def flattenList(S):
if S == []:
return S
if isinstance(S[0], list):
return flattenList(S[0]) + flattenList(S[1:])
return S[:1] + flattenList(S[1:])
mylist = [[[[[1]], [], 4],2,5,[3]]]
print(flattenList(mylist))

**Comment for any further queries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote