Using a list of lists in a function(Method 1)


#1

Hi, I am very stuck and confused on the exercise “Using a list of lists in a function”, could some one help me please.

The instructions:
Create a function called flatten that takes a single list and concatenates all the sublists that are part of it into a single list.

On line 3, define a function called flatten with one argument called lists.
Make a new, empty list called results.
Iterate through lists. Call the looping variable numbers.
Iterate through numbers.
For each number, .append() it to results.
Finally, return results from your function.

The error I get:
flatten([[1, 2], [3, 4]]) returned [[1, 2], [1, 2], [3, 4], [3, 4]] instead of [1, 2, 3, 4]

My code:


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



#2

This is a comparison statement. It should be,

result.append(number)

#4

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