Python Forum

Full Version: Need urgent help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Here is a way
>>> word = "python"
>>> list(word)
['p', 'y', 't', 'h', 'o', 'n']
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.
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'
By array I mean normal lists, and Is there a way I can send you a screenshot of what I mean?
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 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.
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
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 Smile

Alternatively you can do something like this:

>>> answer = 'python'
>>> list_ = [*answer]
>>> list_
['p', 'y', 't', 'h', 'o', 'n']
Thank you guys soo much for the help, it worked!!