0

I have a list which looks like seen=['poll','roll','toll','told']

I need to compare characters from each of the elements from that list.

When I try to strip those charcters using

for i in range(len(seen)):
chain1=[]
for j in range(len(seen)):
    chain1.append(seen[i][j])
print(chain1)

I get an output like this

['p', 'o', 'l', 'l'] ['r', 'o', 'l', 'l'] ['t', 'o', 'l', 'l'] ['t', 'o', 'l', 'd']

Since these are all different lists I cant seem to iterate over them. My thinking is, if I can manage to get those lists into a single list of list I can do my iterations.

Any suggestions on how to make it into a list of list or some other way to iterate over those words?

  • compare characters with whom ? – Chandella07 Jun 08 '21 at 19:06
  • 1
    Do you want to iterate over the four lists simultaneously (use the zip function) or one after another (use itertools.chain.from_iterable)? – Altareos Jun 08 '21 at 19:07
  • Does this answer your question? [How to make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists) – Axe319 Jun 08 '21 at 19:10
  • I need to make to sure that each word is differred only by 1 letter. The given list seen=[] is just an example of how it looks and there might be other lists, too. I need to make sure each sequential word is differred by only one letter and for that I need to compare each chacarcter. –  Jun 08 '21 at 19:13
  • You don't really need to convert the list of strings to a list of lists of characters to iterate over the characters -- strings are iterable – Pranav Hosangadi Jun 08 '21 at 19:34
  • You can use `len` method to get length of the word. So you can iterate through the array and calculate length of every word. – Robert Kwiatkowski Jun 08 '21 at 19:37

1 Answers1

0

you can merge it like below:

seen=['poll','roll','toll','told']

alist=[]
for i in seen:
    chain=[]
    for j in i:
        chain.append(j)
    alist.append(chain)
print(alist)

Output:

[['p', 'o', 'l', 'l'], ['r', 'o', 'l', 'l'], ['t', 'o', 'l', 'l'], ['t', 'o', 'l', 'd']]
Chandella07
  • 2,089
  • 14
  • 22