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

Watch me join a list of integres into a string :)

Hi guys!

He is right when he says that by default you can't simply join a list of integers, but with the powers of type casting(or in this case converting) and list comprehensions(I am pretty sure we will see it later) we can do some crazy things!

Check it out!

string_holder = ""
number_list = [1, 2, 3]

# now for the comprehension

string_holder = " ".join(str(i) for i in number_list)


# now to verify

print(string_holder)

# and the result is >>> "1 2 3"

I love Python

1 Answer

That's an interesting thought!

I never thought of doing that (I always liked using list comprehensions).

I just thought of a way to do it using the map function, which is probably a better choice for this problem:

string_holder = ""
number_list = [1, 2, 3]

# now for the comprehension

string_holder = " ".join(list(map(str, number_list)))

# now to verify

print(string_holder)

# and the result is >>> "1 2 3"

Just a little thing I thought of :P

Using the map function in this case should make the code a little bit less. :)

Try my code in the Console.

~Alex

Wonderful Alex! (yay another Alex! haha)

It is a great solution! Using a more functional approach! I really dig it

And I love list comprehensions, it is arguably one of my favorite features in Python!

Map may be a teeny faster in some cases (when you're NOT making a lambda). List comprehensions may be faster in other cases. Pythonists consider them more direct and clearer.

In this case I believe mapping might be a little faster, and also a little more clear (in this example) :)