Using strings in lists in functions


#1

[](http://)

i don;t know what's wrong with my code still after checking all the similar topic...
could anyone help me ?


#2

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

By appending, You're not joining the strings (items) of list,You're again appending them in a new list and returning a list having same items as original.

Hint**
You have to return final string which is nothing but concatenation of input list items.

further hint**
one approach can be...
going through instruction...


#4

since append function works for lists not strings


#5

instead of result.append(item) try result += item


#6

The method by "result = result + item" works, but I still think there is another way to go.

However it stills off the road...


#7

did anyone answer this?
i see solutions for using the for loop but none with the range method.

did you solve this with this method?


#8

were we never supposed to use .append() in this exercise?


#9

i don't think we're supposed to use results.append() here... since append doesn't work on strings. i'm finding CodeCademy's hints to be very misleading at times. but i'm sure it's for our benefit!


#10

Yep the general rule is not to use append for string - use concatenation instead.
Append is for lists only.


#11

.append() method is defined for list not for string,int,float,tuple or dictionary data types.


#12

n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result = ""
for i in words:
result = result + i
return result

it worked like this


#13

You have to treat result and the inputted thing as separate entities--they follow different rules so you have to address them differently when combining them


#14

Hey, using the loop solution is pretty straight forward! After noticing your question, I thought I'll give it a try with using the range method. Here's my solution:

n = ["Michael", "Lieberman"]
# Add your function here

def join_strings(words):
    result= ""
    for i in range(0, len(words)):
        result += "%s" % words[i]
    return result

print join_strings(n)

It works. Since the range method helps us store the indices of the list in variable the i, we need to use the list[index] method to insert the values at index 0 & index 1 i.e words[i] in the above code.

Hope this helps :slight_smile:


#15

Can someone explain to me why this code is incorrect?

I keep getting the following error:
"Oops, try again. flatten([[1, 2], [3, 4]]) returned [1, 2, 3, 4, 5, 6, 7, 8, 9] instead of [1, 2, 3, 4]"


#16

Disregard the Y I was messing with some other iterations.


#18

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