Using strings in lists in functions


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

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

print join_strings(n)


.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


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:
return results

print flatten(n)


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


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

n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
for word in words:
return result

print join_strings(n)


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?


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