Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:22:58+00:00 2026-06-14T07:22:58+00:00

Working on a function that takes lines of texts and turns them into an

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:23:00+00:00Added an answer on June 14, 2026 at 7:23 am

    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.

    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())
        if (inner_list):
            food_list.append(inner_list)
    
        return food_list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a PHP function that takes a string and converts all of
Hey, I was working with a PHP function that takes multiple arguments and formats
I am working with datetime objects in python. I have a function that takes
I'm working on a function that is too complicated (processor) so I embedded part
I am working on a function that accepts a JSON object as a parameter.
I'm working on a function that handles events from a number of buttons and
I am working on a function that will essentially see which of two ints
I'm working on a search function that finds the song where the search matches
I'm working in PHP and I want to create a function that, given a
I search for the function that run programm by path, and working of main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.