0

I have text file which looks like this:

Cat
Beth
12
female
Dog
Bob
10
male
...
..
.

I want to store it like this: myList = [
["Cat", "Beth", "12", "female"],
["Dog", "Bob", "10", "male"], ...
]
I came up with this idea.
My question: Is there a shorter way to do this?

with open("sample.txt") as myFile:
   myList = []
   temp = []
   bit = 1
   for line in myFile:
       temp.append(line.strip())
       if bit % 4 == 0:
           myList.append(temp)
           temp = []
       bit += 1
Levente Wolf
  • 47
  • 1
  • 5

0 Answers0