Flatten([[1,2],[3,4]]) returned [1] (18/18- Lists and Functions)


#1

Hey, I've been trying to get this working for a few days now, and cannot get anything out of it, I've looked at a similar topic and as far as I can see my code is the same as their solution but I'm still getting an error. My code is:

My Code

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

def flatten(lists):
    results = []
    for numbers in lists:
        for output in numbers:
            results.append(output)
            return results

print flatten(n)

#2

What is the Error? :octopus:


#3

sorry there is no terminal error, the website just keeps saying:
"Oops, try again. flatten([[1, 2], [3, 4]]) returned [1] instead of [1, 2, 3, 4]"


#4

First try avoiding using keywords such as output, second, refresh your webpage, third, "return results" should be in a lower indent like this:

    for output in numbers:
        results.append(output)
    return results

#5

I have something for you, it flattens all lists contained with in no matter the depth and returns the values no matter that whey are in the same order.

This is more of an advanced example, I posted a simpler one after but it only does depth level of one list in a list.

def flatten(a_list):
    """
    Flattens the given list of an arbitrary amount of lists/items into a single list with all values
    :param a_list: Pass the list object you wish to flatten, no list will be left with in. Also maintains order.
    :return: Returns this function with the hold list as an argument if there
    are any list objects, else it returns the held list.
    """
    hold = []
    for item in a_list:
        if list == type(item):
            for it in item:
                hold.append(it)
        else:
            hold.append(item)
    return flatten(hold) if any(type(part) == list for part in hold) else hold

Simple:

def flatten(a_list):
    hold = []
    for item in a_list:
        hold.append(item)
    return hold

Another

def flatten(a_list):
    """
    This one uses list comprehension.
    """
    return [item for item in a_list]

#6

thanks, how's this?

New code

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

print flatten(n)

also there's now a new error:
"Oops, try again. flatten([[1, 2], [3, 4]]) returned [1, 2] instead of [1, 2, 3, 4]"


#7

Indent "return results" one lower :wink: Sorry :stuck_out_tongue:


#8

Your return is still indented too far,

This line

for final_answer in numbers:

will break out when the return is hit after that for is done, you need it after this one

for numbers in lists:

So it should look like

def flatten(lists):
    results = []
    for numbers in lists:
        for final_answer in numbers:
            results.append(final_answer)
    return results

#9

Awwww I think you deserve the "solution" because you put more work in it than I did :wink:


#10

Thank you, but if isn't a stupid question why the if list == type(item):? Is that just to make the code more universal in case its wanted to be used in another less specific situation in which another type of input is used?


#11

Just an excerise, you are overthinking it :smile:


#12

Right sorted thanks :grinning:


#13

thanks that really helped :smiley:


#14

You need the whole line for it to work, it's called a ternary function, meaning it takes three arguments.

return flatten(hold) if any(type(part) == list for part in hold) else hold

So the first thing it see is the return this statement, after that is notices that I have a ternary function and it proceeds to follow the instructions.

in this case I have it doing a check to see if it is a list in any part of the hold object and if there is I pass the hold object back to the function in a recursive function.

If any part of that evaluates True it does the front part which is

return flatten(hold)

Which is the recursive part

Then if that did not return any Trues it does the else statement

Which is

else hold

So it ends up returning hold ending the function and giving us the data.

If you do not understand any part of that ask about it!

Best of luck!


#15

I should also note, ternary functions are used like this, I just did something a little more fancy.

a = 5

b = a if a > 1 else 0

If you don't get this, this is conditional variable assignment which is EPIC, it can remove tons of lines of code with this simple process. Mind you it is not Pythonic to use too much of it.

In conclusion Ternary Functions while not explicitly needed are a useful tool when used properly.

As always, best of luck!


#16

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

Add your function here

def flatten(lists):
1indent results = []
1indent for numbers in lists:
2indent for item in numbers:
3indent results = results.append(item)
1indent return results

print flatten(n)

Gives me this error below. I think it's either a site error or a browser incompatibility at this point. Everything seems to be in place.

Traceback (most recent call last):
File "python", line 12, in
File "python", line 7, in flatten
AttributeError: 'NoneType' object has no attribute 'append'


#17

This should simply be,

results.append(item)

#18

Thanks I was getting frustrated.


#19

yeah, you don't want the "return" phrase to still be indented within a "for" loop