# Using a lists of a list in a function (w/range)

#1

Using a lists of a list in a function

Traceback (most recent call last):
File "python", line 11, in
File "python", line 6, in flatten
TypeError: 'int' object is not iterable

I had already got this to work w/o using range (for numbers in lists) but wanted to try to get it to work with range and I could not. What am I missing? thank you

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

print flatten(n)``````

#2

The first time through your first for..in loop, your `numbers` variable, using n, is going to be set to 0. That is an int. It wouldn't make sense to do `for i in 0`, would it?

#3

If I were to use instead on the second loop range(len(numbers)) it would still be 0? is it possible to use range in this circumstance?

what would numbers be set to in the first loop in the case below. sorry learning

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

print flatten(n)``````

#4

You can stick with your original idea of using a range. You just can't do `for i in numbers:` as your second loop.

Can you see what the values of `numbers` will be each time through the loop?

To test things:
- go here https://repl.it/languages/python
- comment out the bits that are preventing your code from running (that's the `for i in numbers` part)
- any place that you can't figure out a variable's value or if you just want to double check, put a print statement in.

Start with this and make sure you understand what you have going on there so far. Then we can figure what your second loop should be.

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

def flatten(lists):
results = []
print "How many items in our big list?", len(lists)
# first loop gets us the index for each sub list
for numbers in range(len(lists)):
print "numbers: ", numbers
# the second loop is for ???
# for i in numbers:
#     results.append(i)
return results

print flatten(n)``````

#5

when numbers is 0 and 1 it represents the places of the outer list 0: [1,2,3] and 1: [4,5,6,7,8,9]?
so I need to find the length of each of those for the second loop? len(lists[numbers]) ? and thank you for your time!

#6

Yes, that's right.

For your second loop, if you are doing a for..in loop, you don't need to know the length. You'll be doing something like: `for item in the sub-list` -- How are you going to refer to the sub-list using your numbers variable?

#7

I want to say lists[numbers] that would call up lists[0] and lists[1] ? But I am only getting the first list [1,2,3] and not the second part. I'm only looping thru lists[0] it seems.

#8

You have that right. Have another look, if you can't get it, it might be time to post your code again.

#9

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

def flatten(lists):
results = []
print "How many items in our big list?", len(lists)
# first loop gets us the index for each sub list
for numbers in range(len(lists)):
print "numbers: ", numbers
#print "list: ", len(lists[numbers])
# the second loop is for ???
for i in lists[numbers]:
results.append(i)
print numbers
return results

print flatten(n)``````

#10

#11

aha!!! thank you so much, frustrating but rewarding! Now you said I wouldn't need to use in range on the second loop but is it possible to do so as well?

#12

Yes it is, same idea, just getting in deeper. I think it looks messier but as an exercise, you can work on that for a while.

#13

got the extra exercise!! easier now that things seem clearer! thanks again!

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

def flatten(lists):
results = []
print "How many items in our big list?", len(lists)
# first loop gets us the index for each sub list
for numbers in range(len(lists)):
print "numbers: ", numbers
#print "list: ", len(lists[numbers])
# the second loop is for ???
for i in range(len(lists[numbers])):
results.append(lists[numbers][i])
print lists[numbers][i]
return results

print flatten(n)``````

#14

Good for you!

If you like doing this sort of thing, you should start spending some time at https://www.codewars.com -- it will be like this problem, hard at first but then it will get easier.

Also, there is this:

#15

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