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 6026195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:24:38+00:00 2026-05-23T04:24:38+00:00

I have dates in a Python script that I need to work with that

  • 0

I have dates in a Python script that I need to work with that are in a list. I have to keep the format that already exists. The format is YYYY-MM-DD. They are displayed in the form [‘2010-05-12’, ‘2011-04-15’, ‘Date’, ‘2010-04-20’, ‘2010-11-05’] where the order of the dates appears to be random and they are made into lists with seemingly insignificant lengths. The length of this data can get very large. I need to know how to sort these dates into a chronological order and omit the seemingly randomly placed entries of ‘Date’ from this order. Then I need to be able to perform math operations such as moving up and down the list. For example if I have five dates in order I need to be able to take one date and be able to find a date x spaces ahead or behind that date in the order. I’m very new to Python so simpler explanations and implementations are preferred. Let me know if any clarifications are needed. Thanks.

  • 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-05-23T04:24:39+00:00Added an answer on May 23, 2026 at 4:24 am

    You are asking several questions at the same time, so I’ll answer them in order.

    To filter out the "Date" entries, use the filter function like this:

    dates = ['2011-06-18', 'Date', '2010-01-13', '1997-12-01', '2007-08-11']
    dates_filtered = filter(lambda d: d != 'Date', dates)
    

    Or perhaps like this, using Python’s list comprehensions, if you find it easier to understand:

    dates_filtered = [d for d in dates if d != 'Date']
    

    You might want to convert the data types of the date items in your list to the date class to get access to some date-related methods like this:

    from datetime import datetime
    date_objects = [datetime.strptime(x,'%Y-%m-%d').date() for x in dates_filtered]
    

    And to sort the dates you simply use the sort method

    date_objects.sort()
    

    The syntax in Python for accessing items and ranges of items in lists (or any “sequence type”) is quite powerful. You can read more about it here. For example, if you want to access the last two dates in your list you could do something like this:

    print(date_objects[-2:]
    

    If you put it all together you’ll get something like this:

    from datetime import datetime
    dates = ['2011-06-18', 'Date', '2010-01-13', '1997-12-01', '2007-08-11']
    my_dates = [datetime.strptime(d, '%Y-%m-%d').date()
                for d in dates
                if d != 'Date']
    my_dates.sort()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dates in the format 2008-12-23T00:00:00Z . This look a lot like a
I have a Selenium test case that enters dates into a date selector made
I have imported a time series with dates of the following format: test =
I am using MS SQL Server 2005, I have dates stored in epoch time
I have two dates (parsed using str2time ). How can I tell if one
If I have two dates (ex. '8/18/2008' and '9/26/2008' ), what is the best
I have a Table containig Dates, Distances, and TimeForDistance... Is there any way to
I have some Ruby code which takes dates on the command line in the
I have a series of ranges with start dates and end dates. I want
I have an application where the user selects the dates of a first statement

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.