# Why this function works ? task 18/18

#1

Hi guys,

Question as in the title. I'm not sure why function "flatten2" works; (flatten1 & flatten2 return same result)

If I prompt for len() of list 'n' then I get 2. So I feel like I have to iterate loop twice to get into the elements inside any of the 2 lists. I think I'm missing some concept here (?).

I'd be greatful !

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

def flatten1(lists):
results = []
for x in lists:
for items in x:
results.append(items)
return results
print flatten1(n)

def flatten2(lists):
results = []
for numbers in lists:
results += numbers
return results
print flatten2(n)``````

#2

because for flatten2 you use Concatenation

[1, 2, 3] + [4, 5, 6] results in: [1, 2, 3, 4, 5, 6]

python concates the list for you, this is not common, given it is not really fast.Concatenation will give you a new list. if you want to read more google: python list Concatenation

#3

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