Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Set Difference in list of dictionaries
Next article icon

Python | Sort given list of dictionaries by date

Last Updated : 14 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a list of dictionary, the task is to sort the dictionary by date. Let’s see a few methods to solve the task. Method #1: Using naive approach 

Python3




# Python code to demonstrate
# sort a list of dictionary
# where value date is in string
 
# Initialising list of dictionary
ini_list = [{'name':'akash', 'd.o.b':'1997-03-02'},
            {'name':'manjeet', 'd.o.b':'1997-01-04'},
            {'name':'nikhil', 'd.o.b':'1997-09-13'}]
                 
# printing initial list
print ("initial list : ", str(ini_list))
 
# code to sort list on date
ini_list.sort(key = lambda x:x['d.o.b'])
 
# printing final list
print ("result", str(ini_list))
 
 
Output:

initial list : [{‘name’: ‘akash’, ‘d.o.b’: ‘1997-03-02’}, {‘name’: ‘manjeet’, ‘d.o.b’: ‘1997-01-04’}, {‘name’: ‘nikhil’, ‘d.o.b’: ‘1997-09-13’}] result [{‘name’: ‘manjeet’, ‘d.o.b’: ‘1997-01-04’}, {‘name’: ‘akash’, ‘d.o.b’: ‘1997-03-02’}, {‘name’: ‘nikhil’, ‘d.o.b’: ‘1997-09-13’}]

The time complexity of the above Python code is O(n log n), where ‘n’ is the number of dictionaries in the list. 

The auxiliary space complexity of the code is O(1), as it only uses a constant amount of additional space, regardless of the size of the input. 

Method #2: Using datetime.strptime and lambda 

Python3




# Python code to demonstrate
# sort a list of dictionary
# where value date is in a string
 
from datetime import datetime
 
# Initialising list of dictionary
ini_list = [{'name':'akshat', 'd.o.b':'1997-09-01'},
            {'name':'vashu', 'd.o.b':'1997-08-19'},
            {'name':'manjeet', 'd.o.b':'1997-01-04'},
            {'name':'nikhil', 'd.o.b':'1997-09-13'}]
                 
# printing initial list
print ("initial list : ", str(ini_list))
 
# code to sort list on date
ini_list.sort(key = lambda x: datetime.strptime(x['d.o.b'], '%Y-%m-%d'))
 
# printing final list
print ("result", str(ini_list))
 
 
Output:

initial list : [{‘d.o.b’: ‘1997-09-01’, ‘name’: ‘akshat’}, {‘d.o.b’: ‘1997-08-19’, ‘name’: ‘vashu’}, {‘d.o.b’: ‘1997-01-04’, ‘name’: ‘manjeet’}, {‘d.o.b’: ‘1997-09-13’, ‘name’: ‘nikhil’}] result [{‘d.o.b’: ‘1997-01-04’, ‘name’: ‘manjeet’}, {‘d.o.b’: ‘1997-08-19’, ‘name’: ‘vashu’}, {‘d.o.b’: ‘1997-09-01’, ‘name’: ‘akshat’}, {‘d.o.b’: ‘1997-09-13’, ‘name’: ‘nikhil’}]

  Method #3: Using operator.itemgetter 

Python3




# Python code to demonstrate
# sort a list of dictionary
# where value date is in string
 
import operator
 
# Initialising list of dictionary
ini_list = [{'name':'akash', 'd.o.b':'1997-03-02'},
            {'name':'manjeet', 'd.o.b':'1997-01-04'},
            {'name':'nikhil', 'd.o.b':'1997-09-13'}]
                 
# printing initial list
print ("initial list : ", str(ini_list))
 
# code to sort list on date
ini_list.sort(key = operator.itemgetter('d.o.b'))
 
# printing final list
print ("result", str(ini_list))
 
 
Output:

initial list : [{‘d.o.b’: ‘1997-03-02’, ‘name’: ‘akash’}, {‘d.o.b’: ‘1997-01-04’, ‘name’: ‘manjeet’}, {‘d.o.b’: ‘1997-09-13’, ‘name’: ‘nikhil’}] result [{‘d.o.b’: ‘1997-01-04’, ‘name’: ‘manjeet’}, {‘d.o.b’: ‘1997-03-02’, ‘name’: ‘akash’}, {‘d.o.b’: ‘1997-09-13’, ‘name’: ‘nikhil’}]



Next Article
Python | Set Difference in list of dictionaries
author
garg_ak0109
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
  • Python-sort
Practice Tags :
  • python

Similar Reads

  • Sort List of Dictionaries by Multiple Keys - Python
    We are given a list of dictionaries where each dictionary contains various keys and our task is to sort the list by multiple keys. Sorting can be done using different methods such as using the sorted() function, a list of tuples, itemgetter() from the operator module. For example, if we have the fol
    3 min read
  • Sort a List of Python Dictionaries by a Value
    Sorting a list of dictionaries by a specific value is a common task in Python programming. Whether you're dealing with data manipulation, analysis, or simply organizing information, having the ability to sort dictionaries based on a particular key is essential. In this article, we will explore diffe
    3 min read
  • Filter List Of Dictionaries in Python
    Filtering a list of dictionaries is a fundamental programming task that involves selecting specific elements from a collection of dictionaries based on defined criteria. This process is commonly used for data manipulation and extraction, allowing developers to efficiently work with structured data b
    2 min read
  • Python | Set Difference in list of dictionaries
    The difference of two lists have been discussed many times, but sometimes we have a large number of data and we need to find the difference i.e the elements in dict2 not in 1 to reduce the redundancies. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension The na
    3 min read
  • Filter List of Python Dictionaries by Key in Python
    In Python, filtering a list of dictionaries based on a specific key is a common task when working with structured data. In this article, we’ll explore different ways to filter a list of dictionaries by key from the most efficient to the least. Using List Comprehension List comprehension is a concise
    3 min read
  • Python | Sort list of dates given as strings
    To sort a list of dates given as strings in Python, we can convert the date strings to datetime objects for accurate comparison. Once converted, the list can be sorted using Python's built-in sorted() or list.sort() functions. This ensures the dates are sorted chronologically. Using pandas.to_dateti
    3 min read
  • Sorting List of Dictionaries in Descending Order in Python
    The task of sorting a list of dictionaries in descending order involves organizing the dictionaries based on a specific key in reverse order. For example, given a list of dictionaries like a = [{'class': '5', 'section': 3}, {'Class': 'Five', 'section': 7}, {'Class': 'Five', 'section': 2}], the goal
    3 min read
  • Python | Sort dictionary by value list length
    While working with Python, one might come to a problem in which one needs to perform a sort on dictionary list value length. This can be typically in case of scoring or any type of count algorithm. Let's discuss a method by which this task can be performed. Method 1: Using sorted() + join() + lambda
    4 min read
  • Python - Sort dictionaries list by Key's Value list index
    Given list of dictionaries, sort dictionaries on basis of Key's index value. Input : [{"Gfg" : [6, 7, 8], "is" : 9, "best" : 10}, {"Gfg" : [2, 0, 3], "is" : 11, "best" : 19}, {"Gfg" : [4, 6, 9], "is" : 16, "best" : 1}], K = "Gfg", idx = 0 Output : [{'Gfg': [2, 0, 3], 'is': 11, 'best': 19}, {'Gfg': [
    14 min read
  • Python | Sort nested dictionary by key
    Sorting has quite vivid applications and sometimes, we might come up with a problem in which we need to sort the nested dictionary by the nested key. This type of application is popular in web development as JSON format is quite popular. Let's discuss certain ways in which this can be performed. Met
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences