Working on a function that takes lines of texts and turns them into an inner list based
on a space between the blocks. The code works for all but the very last chunk, which it ignores or something.
Code:
def build_grocery_list(grocery_file):
food_list = []
inner_list = []
for line in grocery_file:
if line.strip() == "":
food_list.append(inner_list)
inner_list = []
if line.strip() != "":
inner_list.append(line.strip())
return food_list
returns: [['milk', 'cheese', 'cream cheese', 'eggs'], ['bread', 'buns', 'pita'], ['hamburgers', 'hotdogs', 'chicken'], ['chicken soup', 'canned tuna']]
missing the last block :lettuce, cabbage
If you have an empty line at the end of the text file it will work. If not it never appends the last inner_list. Just add a check and append after you exit the loop.