Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need urgent help
#1
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
Reply
#2
Here is a way
>>> word = "python"
>>> list(word)
['p', 'y', 't', 'h', 'o', 'n']
Reply
#3
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.
Reply
#4
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'
Reply
#5
By array I mean normal lists, and Is there a way I can send you a screenshot of what I mean?
Reply
#6
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.
Reply
#7
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.
Reply
#8
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
Reply
#9
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']
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.
Reply
#10
Thank you guys soo much for the help, it worked!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help urgent fireablade 1 1,502 Oct-01-2019, 05:30 PM
Last Post: buran
  'Hello, World!' Problem - Urgent OmarSinno 7 4,535 Sep-07-2017, 06:22 AM
Last Post: OmarSinno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020