Reputation: 99
For a list of words, how can I access each alphabet of the word. For example
food = ['eel', 'egg', 'yam', 'nut', 'oats']
I need to retrieve each food by its individual letters and make it into a list, i.e.
each_food[1] = ['e', 'e', 'l']
each_food[2] = ['e', 'g', 'g']
Any help is appreciated.
Upvotes: 1
Views: 1460
Reputation: 1
If you need to store the lists in an array named each_food
, you can use a for loop to split the string into a list of individual characters and then append each list of characters to each_food
:
food = ['eel', 'egg', 'yam', 'nut', 'oats']
each_food = []
for item in food:
each_food.append(list(item))
This way you can access each list of characters with the corresponding string index (starting from 0). For example:
each_food[0]
Would produce the first list as output:
['e', 'e', 'l']
Upvotes: 0
Reputation: 520
list(<string>)
will split a string into a list of characters
food = ['eel', 'egg', 'yam', 'nut', 'oats']
print(list(food[0]))
print(list(food[1]))
Upvotes: 3