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 Collections (2016, retired 2019) Lists Disemvowel

Chelsea Yang
Chelsea Yang
2,982 Points

Why [word] doesn't work while list(word) does, and why str(list) doesn't work while ''.join(list) works

Hello community, when I'm doing the Disemvowel challenge, I tried to convert string into list, I found L = [word] is not giving the output as I want, but L = list(word) does. Same confusion for trying to convert list back to string, str(L) not working but ''.join(L) works. I saw many people use [word] and str(L) online but not clear why it doesn't work here, can anyone help? Thank you!

1 Answer

Steven Parker
Steven Parker
231,269 Points

These do different things. Placing braces around a string variable creates a list with that string as a single item, but using the "list" function converts it into a list where each letter is a separate item:

So if word = "test", then "[word]" is "['test']", but "list(word)" is "['t', 'e', 's', 't']"

Then, creating a string from a list with "str" gives you one that shows each list part, but using "join" puts it back together:

So if "test = ['t', 'e', 's', 't']", then "str(test)" is "['t', 'e', 's', 't']", but """.join(test)" is just "test"

Chelsea Yang
Chelsea Yang
2,982 Points

Thank you for the detailed explanation Steven!