Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

I am trying to use the " ".join method on a list of numbers, and its not working. help?

what should I do in order to go from [3,2,5] to "3 2 5", for example. Thanks.

functions.py
# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.

def add_list(x):
  total = 0
  for y in x:
    total = total + y
  return total

def summarize(x):
  total = add_list(x)

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

list in Python has no .join() method.

However, you can do

# convert list to string
str([2,3,4])   #=> "[2, 3, 4]"

this should be enough to help you solve the problem.

Thanks! But how do i convert [3,4,3] to "3 5 6" (not "[2,5,2]")

William Li
William Li
Courses Plus Student 26,868 Points

I failed to see why you wanna convert it to 3 5 6, the example output of summarize is "The sum of [1, 2, 3] is 6." converting the list to 3 5 6 doesn't help you in implementing the summarize function, it will in fact make it a lot more difficult.

ok, now i see. thanks a lot!

William Li
William Li
Courses Plus Student 26,868 Points

yes, depending on how you format that string, if you wanna combine them using +, you have to use str() for the list, otherwise

def summarize(x):
  return "The sum of {} is {}.".format(x, sum(x))