I'm reading lines from a text file, then inserting them into a list in 2 char pairs
eg hello = ['he','el','ll','lo']
.
In my current code, the initial read of the lines turns each line into its own list, then puts THAT into a list, giving me a list of lists. Thus the two lines
hello
world
give me the list of lists [[['he','el','ll','lo'], ['wo','or','rl','ld']]
. I can take this code and singleList = sum(listlist,[])
which will give me a single list, however this is inefficient due to it iterating over the list twice (and I read a note that said this is a bad way to do it in the first place).
How can I take my code, and input the values from my lines into a single list on the first pass?
def countPairs():
print ()
inFile = open("hello.txt", "r")
n = 2
linsiz = []
for line in inFile:
line.rstrip('\n')
linsiz.append([line[i:i+2] for i in range(0,len(line),1)])
print (linsiz)
singleList = sum(linsiz,[])
print (singleList)
countPairs()
Whoever suggested the possible duplicate is tryharding to shut down threads. That doesn't even remotely answer my question. I'm quite convinced they didn't even read my post, and instead recognized something that had hello = ['he','el','ll','lo']
and thought "low point poster, lets shut it down". Idiotic.