-2

I'm having trouble converting a input into a list in python.

AYGSU
DOMRA
CPFAS
XBODG
WDYPK
PRXWO

What I need is to convert the code above into a 6x5 list.

Jan
  • 42,290
  • 8
  • 54
  • 79

1 Answers1

0

Here you can take advantage of the .split function for string objects:

list_result = [[c for c in x] for x in input().split()]
"""
Result:
[['A', 'Y', 'G', 'S', 'U'],
 ['D', 'O', 'M', 'R', 'A'],
 ['C', 'P', 'F', 'A', 'S'],
 ['X', 'B', 'O', 'D', 'G'],
 ['W', 'D', 'Y', 'P', 'K'],
 ['P', 'R', 'X', 'W', 'O']]
"""

I'm using two list comprehensions. The first goes through and splits your input into individual words, the inner list comprehension splits each word into a list of its characters.

M Z
  • 4,571
  • 2
  • 13
  • 27
  • It seems more of an `add` to me, really. – Jan Aug 15 '21 at 18:23
  • @Jan I'm not familiar with that, could you specify? I actually have no clue and would love to see what you're talking about. – M Z Aug 15 '21 at 18:26
  • This assumes that the input is split on spaces and a singular input – Sayse Aug 15 '21 at 18:27
  • @Sayse the original question was written where the input was such, if I recall. It also doesn't need to be spaces, it just needs to be in one string – M Z Aug 15 '21 at 18:28
  • @MZ - It may have appeared as a single string but if you look at the raw text via the editor all that Jan did was put it in a code block as is – Sayse Aug 15 '21 at 18:30
  • 1
    @Sayse Ah, ok. Well my statement still stands - if it was a single string value, split also takes care of newline characters. But its good to know that it was split like that – M Z Aug 15 '21 at 18:31