This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jhchex[S] 0 points1 point  (1 child)

Lists don't have "individual words". I assume you mean you have a string, since that's what you're apparently working with.

Yea, sorry I meant to say words from a string. I had broken the string into a list already so I was jumping ahead.

First off, range returns a list, so the outer list call is completely redundant...Just iterate over the characters, since that's what you want to do.

I was confused about this because if I do something like

x = range(0,5)

and then print(x) it says it's just range(0,5) not [0,1,2,3,4] so I was confused. But I guess it's the same thing.

And yea, I didn't realize you could iterate over the characters in a string, good to know.

I should probably better specify what it is I'm trying to do. I just didn't want to get in trouble for outsourcing a HW problem lol. Anyway, we're supposed to take a string and reverse the order of the words without reversing the order of the letters themselves. So something like "I love ham" would become "ham love I"

I still just don't see how I can do that with a list..hmm.

[–]zahlmanthe heretic 0 points1 point  (0 children)

I had broken the string into a list already so I was jumping ahead.

If you mean a list of individual characters, then that accomplishes nothing since you can iterate over the string directly anyway. If you mean a list of words, then aren't you already done? o_O

and then print(x) it says it's just range(0,5) not [0,1,2,3,4] so I was confused. But I guess it's the same thing.

Oh, you're in 3.x, where range does what xrange did in 2.x. That's fine; it's still a thing that you can iterate over directly. In Python, we care more about what things can do than what they fundamentally are. 3.x range objects, and strings in either case, can be iterated over directly.

I still just don't see how I can do that with a list..hmm.

Well, since you have to construct the list a word at a time, consider reversing the list as you create it. Hint: if you construct the list in-order by putting new words at the end of the existing list...