12. Lists and Functions


#1



n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for i in range(len(words)):
        words.append(result)
        return result


print join_strings(n)

""""Hey Guys I've already completed the course I just wanted to figure out why the .append() method doesn't seem to work. Please Help."""


#2

your result is empty string and you are appending empty string. Maybe you want to append i?


#3

Thanks! Have just tried your method. But now its giving me this error:

Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned '' instead of 'xyza'.


#4

Tried it on IDLE too


#5

append to result and not to words:

result.append(i)


#6

You need to append your i to result to get string

reuslt.append(i)

#7

I've tried the results.append(i) method getting new error. @knezknele @tomaszd7

Oops, try again. join_strings(['x', 'y', 'z', 'a']) resulted in an error: 'str' object has no attribute 'append'


#8

And also trying to run this other code on IDLE please tell me what I'm dooing wrong. I want to create a single list out of the sub lists.

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

def flatten(lists):
results = []
for lists in n:
for numbers in lists:
result += lists[numbers]
return results

print flatten(n)


#9

your variable is called result not results


#10

Haha yea that was a typo. I wrote result but still gave same error.


#11

Found solution to that other piece of code of concatenating sub lists. but still stuck on joining the strings. @knezknele @tomaszd7


#12

Oh, your return should be outside of the loop and you can't append string, it just works for list. You can try something like this

result += words[i]

#13

Thank you very much! @knezknele