I cannot find the error in my code


#1



Traceback (most recent call last):
File "python", line 12, in
File "python", line 7, in flatten
AttributeError: 'str' object has no attribute 'append'


Replace this line with your code. 
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists) :
    results =''
    for number in lists :
        for i in range(len(number)) :
            results.append(i)
            
    return results


print flatten(n)


#2

append() is for lists. You created results as an empty string so append() won't work for it.
Do you mean to create it as an empty list?


#3

results = '' should be results = [].

For every sublist in lists and for every number in sublist, append the number to results, like so:

for sublist in lists:
    for num in sublist:
        results.append(num)

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.