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 trialKevin Donaldson
261 PointsHow to list and split
It giving me a feedback that my name is incorrect.
full_name = "Kevin Donaldson"
name_list = list ("Kevin Donaldson")
name_list.split()
3 Answers
Lewis Cowles
74,902 PointsHi, definitely when confused the Python docs should be your first port of call. Especially if you are aware of, or code in many languages, https://docs.python.org/2/library/string.html
The answer is rather simple (sorry)
full_name = "Kevin Donaldson"
name_list = full_name.split(" ")
The thing to take from this, is you were along the right lines. you knew to use split, but the syntax needed work. Good luck in your python adventure, and hope you have fun!
Ryan Ruscett
23,309 PointsI gotcha man, I agree! Sometimes I get ahead of myself and just start typing what comes to mind lol.
Ryan Ruscett
23,309 PointsHey,
Check this out.
full_name = "Kevin Donaldson"
name_list = list("Kevin Donaldson")
print name_list # < ----- If I print the name_list here. I already get a string split up into a bunch of pieces or AKA a List
OUTPUT
['K', 'e', 'v', 'i', 'n', ' ', 'D', 'o', 'n', 'a', 'l', 'd', 's', 'o', 'n']
Now, you are trying to split a list. List has no split()
. So it's failing.
Now, you are repeating code here. Your name. How about using the variable for your name instead of typing it twice. Like so.
full_name = "Kevin Donaldson"
name_list = list(full_name )
You get the same result but less typing lol. Take it for what it's worth. But notice the OUTPUT again. It is the same but notice 1 thing about it.
['K', 'e', 'v', 'i', 'n', ' <Space>', 'D', 'o', 'n', 'a', 'l', 'd', 's', 'o', 'n']
There is a space in it. So we need to split the string by it's spaces, then we need to put it in the list.
full_name = "Kevin Donaldson"
name_list = list(full_name.split(" ")) # < --- Will take full_name a string. Split it at the space between first and last name, then it will create a list. The response to this is now the following
['Kevin ', 'Donaldson']
You split a string into two pieces and make a list out of two pieces. Instead of making a list out of your name and then splitting the list. Which is what you were trying to do.
With a string it's one sequence so to speak so when you list a string. It splits it up into individual letters. Since a string is stored as one piece of data. The list method just breaks all the pieces up for a list. Since technically there is only one piece of data. Can't make a list out of one piece of data, or else it would just be one piece of data. Why bother with the list. So we split it first into two pieces and then list it and we get a list of two pieces and not one piece broken up to fit into a list.
Does this make sense? Let me know if not and I can try to clarify.
Lewis Cowles
74,902 PointsRyan, you clearly went to a lot of effort, please surround your code with as if it were code (three grave-ascent characters and the language to open, three grave-ascent characters to end). Also it will already be a list after split, please read the python docs. The working code is above in my answer buddy ;)
Ryan Ruscett
23,309 PointsHey,
I didn't say your code was wrong. In fact it does the exact same thing as mine. That wasn't the question though.
The question was why her name was coming back incorrect. After running her code I get this error.
AttributeError: 'list' object has no attribute 'split'
Now yes split returns a list. Going off the principal of Operator Precedence. Whatever is in () will be done first. So even if you put list(something) if that something has computation. It is done first. So I was trying to give more detail on her issue and the idea surrounding it instead of the answer or just saying split() returns list.
If the forum just contained only answers. What is to keep someone from just looking up and pasting your code. At least we can offer some sort of reasoning. Reasoning is subjective, if there was only one way to think of something there would be only one Java book or Python book, but there are hundreds.
In either case, thanks for the heads up on how to make code examples look better in the forum.
Lewis Cowles
74,902 PointsHi Ryan,
I disagree on what the question was, but thanks for getting back to me, and letting me know where you are coming from. It's okay for us to disagree about our answers, and to cover different points, but I would urge you to please take a look into language interpreters a little deeper. The redundant list call is unlikely to be stripped from the compiler (its explicit and is unnesecarry), and altough it may have little effect; in a large application, not putting things you don't need to (such as telling lists to be lists), will save your code time, to spend on other things, or just result in more free host machine resources.
I'm also not saying the forum should just contain answers, or that it's a massive deal, it's not, but I didn't want OP to get lost in all the jargon. If you'll notice, I linked the Python Manual, which has hundreds of examples of how to use Python, expands upon string functions and joining, used in the next part of the stage, and at least one example for most method calls; because I am aware that there are hundreds of books, I reveiwed a few, and one thing that I think more books could do with is employing the DRY principle, using more inter-textual references, and a little less "here is another way to do this/that thing.", that is not an improvement.
Have a great day!
Iain Simmons
Treehouse Moderator 32,305 PointsAdjusted the answer from Ryan Ruscett for code formatting (including comments).
Kevin Donaldson
261 PointsKevin Donaldson
261 PointsThanks so much, your code worked perfectly well.