i need to create a python function that reads and lists groceries from a file called “food.txt” such that when ever a new line in the textfile is found, a new list grouping within the general list is created.
food.txt:
milk
cheese
bread
steak
chicken
potatoes
^ each word should be on its own line with a single new line between the groups
output: [[‘milk’,’cheese’,’bread’],[‘steak’,’chicken’,’potatoes’]]
so far i have:
def build_grocery_list(file_name):
outer_list=[]
inner_list=[]
food_list=open(file_name,"r")
for line in food_list:
line.strip('\n') # no white spaces in the list
Try this (here is a gist):