18. Using List of Lists in a Function by Method 2 only

Hi guys, I’m trying to use only Method 2 in solving this problem. I read through various threads but cannot seem to find the definitive answer. Here’s my code, I think the error lies somewhere in append section, but cannot figure it out.

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

print flatten(n)

In its simplest form, as above, a list of lists, 2D, we can use list concatenation.

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
     result = []
     for elem in lists:
          result += elem
     return result

print flatten(n)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

This doesn’t answer OP’s question as they wanted to use method 2 (for x in range(len(y)))

A post was split to a new topic: Should I change my avatar?

It does, there is a valuable lesson to be learned here.
One of coding efficient.
In this case, there is the opportunity to only use one for loop instead of two. This is much easier, clearer and even a (very little) bit more efficient.

In short: you should always try to look for the easiest or most efficient way when programming.

1 Like

And you decided to pull my chain rather than provide a response of your own? If the learner doesn’t post a link are we supposed waste our time tracking down the lesson? Where is this ‘method 2’ the OP (and now you) refers to?

This topic was started and replied to 5 days ago, and abandoned by the OP. Can I take that to mean they found a clue to the answer in my post (concatenation of lists)?

The second method that gogocitygirl99 is referring to is using range(len(list_name)) as opposed to just list_name when iterating through the lists.

Still don’t know which exercise this is. Why is everyone looking at me, rather than just answering the OP? It’s bad enough the OP doesn’t appear to be watching this topic. We are just spinning our wheels here.

The exercise is the title of the post… o_O

We don’t have exercises on speed dial. Either post a link or let this one go, please.

Found the link.

1 Like

Given the following 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.

It should be fair to iterate the outer object without using a range…

def flatten(lists):
    results = []
    for numbers in lists:
        for x in range(len(numbers)):
    return results
print flatten(n)

Use a look up for the outer loop so numbers is an item from the list, not an index.

    for numbers in lists:

I see ,thanks a lot !

1 Like

2 posts were split to a new topic: Why did the len() format not work for the outer loop

A post was split to a new topic: Problema porque nod evuelve las dos listas