Problem 1 Programming Problem 1 Write a function flatten that takes a list of at
ID: 3802678 • Letter: P
Question
Problem 1 Programming Problem 1 Write a function flatten that takes a list of atoms or lists (that can be of atoms or lists) and returns a flattened list. An atom is an element that cannot be broken down further into constituent pieces. The code below illustrates how this function should perform. Put your code for this function in a new module named FunctionFun.py Listing 1: Implementing Flatten. 1 x [11, 2], 13,4], 5 [6]],7] 3 z [0, 1 "small", 5, 23], "car "JJ 6 def flatten (x) 7 TODO: implement the flatten function 9 print flatten (x))Explanation / Answer
Answer
def flatten(x):
y = []
for item in x:
if (isinstance(item, (tuple, list))):
y.extend(flatten(item))
else:
y.append(item)
return y
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.