Posts: 5
Threads: 1
Joined: Nov 2020
Hi,
How to take a word and put the letters into separate indexes of an array instead of putting the letters into 1 index?
Thanks
Posts: 4,790
Threads: 76
Joined: Jan 2018
Here is a way >>> word = "python"
>>> list(word)
['p', 'y', 't', 'h', 'o', 'n']
Posts: 5
Threads: 1
Joined: Nov 2020
Yes, but then if I want to read the word into an array, than it reads all the letters into 1 index instead of putting the letters into separate indexes.
Posts: 4,790
Threads: 76
Joined: Jan 2018
I'm using ordinary Python lists, I don't know what you mean by 'array'
>>> word = "python"
>>> L = list(word)
>>> L
['p', 'y', 't', 'h', 'o', 'n']
>>> ''.join(L)
'python'
Posts: 5
Threads: 1
Joined: Nov 2020
By array I mean normal lists, and Is there a way I can send you a screenshot of what I mean?
Posts: 1,950
Threads: 8
Joined: Jun 2018
Let's take ambiguity out of the way. Are we talking about arrays or lists?
Other than that:
>>> list_ = list('python')
>>> for item in list_:
... print(f'Letter {item} is at index {list_.index(item)}')
...
Letter p is at index 0
Letter y is at index 1
Letter t is at index 2
Letter h is at index 3
Letter o is at index 4
Letter n is at index 5
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 5
Threads: 1
Joined: Nov 2020
Nov-24-2020, 07:58 AM
(This post was last modified: Nov-24-2020, 07:59 AM by OneTwo.)
I mean lists, this is it:
n=input()
t=[]
t.append(list(n))
and than all the letters of the word go in the first index.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Don't append it (which adds the list into a single element), extend it.
word_letters = list("python")
t = []
t.append(word_letters)
print(f"After append, the first element is {t[0]}")
t = []
t.extend(word_letters)
print(f"After extend, the first element is {t[0]}") Output: After append, the first element is ['p', 'y', 't', 'h', 'o', 'n']
After extend, the first element is p
Posts: 1,950
Threads: 8
Joined: Jun 2018
Nov-24-2020, 08:39 AM
(This post was last modified: Nov-24-2020, 08:39 AM by perfringo.)
There is method for that. And this ain't append. It's extend:
>>> list_ = list('python')
>>> my_list = []
>>> my_list.extend(list_)
>>> my_list
['p', 'y', 't', 'h', 'o', 'n'] EDIT: ninjad by bowlofred
Alternatively you can do something like this:
>>> answer = 'python'
>>> list_ = [*answer]
>>> list_
['p', 'y', 't', 'h', 'o', 'n']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 5
Threads: 1
Joined: Nov 2020
Thank you guys soo much for the help, it worked!!
|