List Index out of range error


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096#

I am getting an error message saying that IndexError: list index out of range

I don't understand where am i going wrong


n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
    results = []
    for numbers in lists:
        for i in numbers:
             results.append(numbers[i])
    return results
print flatten(n)


#2

Well think about what the for loops do:

for numbers in lists:

this gives you the two entries of n which are:

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

Now this:

for i in numbers:

gives you the entries of the sub-list:

1
2
3

These are the values not positions on which you can find the values so:

numbers[1] = 2
numbers[2] = 3
numbers[3] -> IndexError there is no 4. element in numbers.

so instead of numbers[i] why not just append i?


#6

Yes.Got it.
Thanks a lot!