Using strings in lists in functions


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/5?curriculum_id=4f89dab3d788890003000096


Traceback (most recent call last):
File "python", line 8, in
File "python", line 5, in join_strings
MemoryError


n = ["Michael", "Lieberman"]
def join_strings(words):
    result = ""
    for item in words:
        words.append(item)
    return result

print join_strings(n)


#2

.append() is a built in function to append items to a list, to join strings use +:

print "Michael" + "Lieberman"

above is a hint how to do this, see if you can fit the last pieces of the puzzle together


#3

Can you help with this one too,

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for items in lists:
for numbers in items:
lists.append(results)
return results

print flatten(n)


#4

see this code: nameOfList.append(itemAppending)

the variable before .append() is the list your appending to, the item between the parentheses is what you append to the list. numbers contain the numbers you want to append results


#5

I am not getting the hint :slight_smile: I tried this:

n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result=""
for word in words:
result=words.append(word)
return result

print join_strings(n)


#6

but what do you not get about the hint? append is clearly wrong, given you want a string as result. (see explanation about append)

lets do a different example then:

name1 = "Michael"
name2 = "Lieberman"
result = name1 + name2

now, how could you use a loop, to use + to join the items in the list together?


#7

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