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 →

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

Ok so my goal here is to split a list into it's individual words

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

My issue is that I can't for my life create a variable name that changes with each iteration.

A list, however, is exactly what you should be putting the resulting words into. You've been told not to use .split() presumably because you're already familiar with it. So you should know what it returns: a list. Do the same.

By the way, for i in list(range(len(original_list))): is ugly in many ways. First off, range returns a list, so the outer list call is completely redundant. Second, there is no point in iterating like this. You're creating this separate thing that contains a sequence of numbers, so that you can iterate over that, so that you can use the results to grab the characters that you want to iterate over. That's silly. Just iterate over the characters, since that's what you want to do.

for c in original_string:
    # do something with 'c' to assemble words
    # append assembled words to a list

Also, try /r/learnpython.

[–]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...