I know how the extend() method works. But when it comes to this program I am not able to figure out the flow of execution. Could | Sololearn: Learn to code for FREE!

0

I know how the extend() method works. But when it comes to this program I am not able to figure out the flow of execution. Could

def flatten(arr): resultArr = [] for custItem in arr: if type(custItem) is list: resultArr.extend(flatten(custItem)) else: resultArr.append(custItem) return resultArr print(flatten([[1,2,[3]]])) output- [1,2,3]

8/7/2022 4:12:32 PM

Levi

2 Answers

New Answer

0

It's just recursion, the code will check for the type of each element of a given list, and if this type is `list`: the function will call itself to work on this new `list` applying the same rule until it does not extend anymore, so it will just consume every list it founds before jumping to the next element.

0

Ervis Meta - SoloHelper Could u pls explain with an example. It will be really helpful !