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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:52:54+00:00 2026-06-05T21:52:54+00:00

I’m pretty new to python (couple weeks into it) and I’m having some trouble

  • 0

I’m pretty new to python (couple weeks into it) and I’m having some trouble wrapping my head around data structures. What I’ve done so far is extract text line-by-line from a .txt file and store them into a dictionary with the key as animal, for example.

database = {
    'dog': ['apple', 'dog', '2012-06-12-08-12-59'],
    'cat': [
        ['orange', 'cat', '2012-06-11-18-33-12'],
        ['blue', 'cat', '2012-06-13-03-23-48']
    ],
    'frog': ['kiwi', 'frog', '2012-06-12-17-12-44'],
    'cow': [
        ['pear', 'ant', '2012-06-12-14-02-30'],
        ['plum', 'cow', '2012-06-12-23-27-14']
    ]
} 

# year-month-day-hour-min-sec                                       

That way, when I print my dictionary out, it prints out by animal types, and the newest dates first.

Whats the best way to go about sorting this data by time? I’m on python 2.7. What I’m thinking is

for each key:

grab the list (or list of lists) –> get the 3rd entry –> '-'.split it, –> then maybe try the sorted(parameters)

I’m just not really sure how to go about this…

  • 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-05T21:52:56+00:00Added an answer on June 5, 2026 at 9:52 pm

    Walk through the elements of your dictionary. For each value, run sorted on your list of lists, and tell the sorting algorithm to use the third field of the list as the “key” element. This key element is what is used to compare values to other elements in the list in order to ascertain sort order. To tell sorted which element of your lists to sort with, use operator.itemgetter to specify the third element.

    Since your timestamps are rigidly structured and each character in the timestamp is more temporally significant than the next one, you can sort them naturally, like strings – you don’t need to convert them to times.

    # Dictionary stored in d
    from operator import itemgetter
    # Iterate over the elements of the dictionary; below, by
    # calling items(), k gets the key value of an entry and 
    # v gets the value of that entry
    for k,v in d.items():
        if v and isinstance(v[0], list):
            v.sort(key=itemgetter(2)) # Start with 0, so third element is 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.