f = open("sonad.txt",encoding="utf-8")
c = f.readlines()
blokk = [[]] * 15
for read in c:
length = len(read.strip())
blokk[length].append(read.strip())
sonad.txt has just some random words and I would like to put them in an order like so:
All the words, that are 1 letter long go to blokk[1]
2 letters long go to blokk[2]
And so on…
But what my current code does is that it adds an element to allL block[x] so that
blokk[1] blokk[2] blokk[3] ….. are all the same.
The line
blokk = [[]] * 15creates a list that has the same empty list in it 15 times.You want to use a list comprehension instead:
Try it out for yourself: