Home > Blockchain >  How to get each letter of word list python
How to get each letter of word list python

Time:02-10

l = ['hello', 'world', 'monday']
        for i in range(n):
            word = input()
            l.append(word)
        for j in l[0]:
            print(j)
    Output : h e l l o

I would like to do it for every word in l.

I want to keep my list intact because i would need to get len() of each word and i won't know the number of word that i could possibly get. I don't know if i'm clear enough, if you need more informations let me know, thanks !

CodePudding user response:

def split_into_letters(word):
    return ' '.join(word)
lst = ['hello', 'world', 'monday']
lst_2 = list(map(split_into_letters, lst))
print(lst_2)

You can map each word to a function that splits it into letters

CodePudding user response:

l = ['hello', 'world', 'monday']
list(map(list, l))
#output
[['h', 'e', 'l', 'l', 'o'],
 ['w', 'o', 'r', 'l', 'd'],
 ['m', 'o', 'n', 'd', 'a', 'y']]

CodePudding user response:

from itertools import chain


lst = ['hello', 'world', 'monday']


# Print all letters of all words seperated by spaces
print(*chain.from_iterable(lst))


# Print all letters of all words seperated by spaces
# for each word on a new line
for word in lst:
    print(*word)
  • Related