Using a list of a list in a function


#1

I’m on exercise 18 of ‘Using a list of lists in a function’ and I’m sure my thinking is correct but there’s an error in the actual doing of it!

My code below returns:

flatten([[1, 2], [3, 4]]) returned [1, 2] instead of [1, 2, 3, 4]

My thinking (and reading through these forums I’m not just quitting first time I get stuck!) is that I create my first list which gets all my numbers in one bunch, I then go through that new combined list and append them to my results.
I did think that it was maybe my return was indented incorrectly as is looks below but on the code entry section it is inline with my first for loop (which I think is where it needs to be to get all the numbers).

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
  results = []
  for list_2 in lists:
  	for number in list_2:
    		results.append(number)
	return results



print flatten(n)

Below is my code showing the indentation (I’ve been bitten by this before, it’s not the same as SQL! :thinking:)
image


#2

Yes, the return statement should be outside the for loops, and therefore should be aligned with the header of the outer loop. However, you used both tabs and spaces for indentation, which causes problems in Python programs. Instead, use one or the other. For Codecademy exercises, use spaces rather than tabs. To correct your code for the current exercise, redo all the indentations, using only spaces, with two spaces per level of indentation.