I’m trying to write a function that will open a .txt file in the shell as a list of list of str.
The file is something like:
TYUIO
GHJKL
BNMCV
ASDFG
XCVBN
And I need my function to return:
[['T', 'Y', 'U', 'I', 'O'], ['G', 'H', 'J', 'K', 'L'], ...]
and so on.
The function I have written almost produces this. It returns:
[['TYUIO'], ['GHJKL'], ...]
and so on. What do I need to change/add to this function to make it return to correct nested list?
This is my function:
board_list = []
for line in board_file:
items = line.rstrip('\n').split('\t')
items = [item.strip() for item in items]
board_list.append(items)
return board_list
Can anyone help?
Try using
list()andextend().You can also do it in one line, assuming each line contains a single word. I’m not sure why you are splitting on
\tso this might not work for you.