Using a list of lists - Help

python

#1

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

def flatten(lists):
results = []
for numbers in lists:
for number in numbers:
results.append(number)
return results

print flatten(n)

help plz


#2

You only require one for loop to perform this operation, this in turn will make your code cleaner and more efficient

so you can discard this loop,

in doing so also just use the += operator instead of appending the results into your empty list because the append() method will not merge your lists,

results += numbers

other than those altercations your code is fine. :slight_smile:


#3

thank you sssssssssssssssss


#4

You're welcome :slight_smile:


#5

still getting an error
Oops, try again. flatten([[1, 2], [3, 4]]) returned [1, 2] instead of [1, 2, 3, 4]
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

def flatten(lists):
results = []
for numbers in lists:
results += numbers
return results

print flatten(n)


#6

Is your code indented like this,

def flatten(lists):
    results = []
    for numbers in lists:
        results += numbers
    return results




print flatten(n)

#7

lol the return was wrong indent :slight_smile:


#8

So it works as it should now? :slight_smile:


#9

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