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

  • Home
  • SEARCH
  • 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 8922355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:48:45+00:00 2026-06-15T06:48:45+00:00

I have a function that filters list items if their date is in the

  • 0

I have a function that filters list items if their date is in the past (smaller than current date).

meetings = []
def clean_old():
    meetings = [meeting for meeting in meetings if time.mktime(meeting) >= time.localtime()]

When the list is empty, this code crashes.

Why does it crash? It says for meeting in meetings, and if meetings is empty then everything should be alright.

How do I fix it and what is the explanation for that occurrence?

  • 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-15T06:48:46+00:00Added an answer on June 15, 2026 at 6:48 am

    I assume that you are seeing this exception:

    UnboundLocalError: local variable 'meetings' referenced before assignment
    

    What you’re experiencing here doesn’t actually have anything to do with list comprehensions. This error occurs because you initially defined meetings outside of your function, but you are trying to assign it a new value inside the function.

    When Python sees that a variable is assigned a value inside a function, it treats that as a new variable, specific to that function. This prevents the function from accessing any variables from outside of the function that have the same name.

    Internally, Python is doing something sort-of like this:

    meetings_outside = []
    
    def clean_old():
        meetings_inside = [meeting for meeting in meetings_inside if time.mktime(meeting) >= time.localtime()]
    

    You can understand why this would fail: meetings_inside is being defined in terms of itself. When Python tries to look up the value of meetings_inside to begin iterating over its content, it fails because it hasn’t been assigned a value yet.

    How to deal with this depends on which version of Python you are using, and there the initial value of meetings is defined.

    In Python 3, you can simply add nonlocal meetings to the top of your function. This will tell it that you are referring to an existing variable named meetings, not creating another one.

    However you are probably using Python 2, which does not have the nonlocal keyword. It does have the global keyword, which does the same thing but only if meetings is defined at the top level of a module: outside of any other functions or classes.

    For example, this would work in Python 2 if you had nothing else in your file:

    import time

    meetings = []
    
    def clean_old():
        global meetings
        meetings = [meeting for meeting in meetings if time.mktime(meeting) >= time.localtime()]
    
    clean_old()
    print meetings
    
    []
    

    However this would not, because meetings is defined inside of a function:

    import time
    
    def main():
        meetings = []
    
        def clean_old():
            global meetings
            meetings = [meeting for meeting in meetings if time.mktime(meeting) >= time.localtime()]
    
        clean_old()
        print meetings
    
    main()
    
    NameError: global name 'meetings' is not defined
    

    You would need to work around the issue. The simplest way would be to modify the assignment to be:

     meetings[:] = [meeting for meeting in meetings if time.mktime(meeting) >= time.localtime()]
    

    This tells Python that you are replacing all of the values inside of meetings, but aren’t creating a new list object. (The : syntax is called “slicing” and is partially described in the Python tutorial.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have a function that returns two values in a list. Both values need
I have a list of Filters that are passed into a webservice and I
I have a few divs that contain unordered lists. Inside those list-items I have
I have been fruitlessly trying for several hours to make a function that filter
I have a control that has a Filter property that expects a function that
Inside my Controller i have function that runs after user clicks on item, which
In my Java code I have function that gets file from the client in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have a function that takes an input string and then runs the string

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.