Python Forum
Easy way to sort a nested dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Easy way to sort a nested dict
#1
I want to convert a dict of audio metadata into an ordered dict, sorted by it's nested values (such as values of keys 'artist' or 'track'). What would be the fastest way to achieve this?

The database is organized like this:

#!/usr/bin/python3
from collections import OrderedDict

db = \
{
  "/data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-17 Disc Wars.mp3": {
    "duration": "00:04:11",
    "artist": "Daft Punk",
    "album": "Tron: Legacy (Cd1)",
    "track": "17",
    "title": "Disc Wars"
  },
  "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/07 - Godichons.mp3": {
    "duration": "00:05:09",
    "artist": "Keith Kouna",
    "album": "Les ann\u00e9es monsieur",
    "track": "7",
    "title": "Godichons"
  },
  "/data/Music/Daft Punk/Tron_ Legacy (Cd1)/1-07 Rinzler.mp3": {
    "duration": "00:02:17",
    "artist": "Daft Punk",
    "album": "Tron: Legacy (Cd1)",
    "track": "07",
    "title": "Rinzler"
  },
  "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/10 - Le Tape.mp3": {
    "duration": "00:06:06",
    "artist": "Keith Kouna",
    "album": "Les ann\u00e9es monsieur",
    "track": "10",
    "title": "Le Tape"
  },
  "/data/Music/Keith Kouna/Les ann\u00e9es monsieur/09 - L'or.mp3": {
    "duration": "00:05:05",
    "artist": "Keith Kouna",
    "album": "Les ann\u00e9es monsieur",
    "track": "9",
    "title": "L'or"
  }
}

db = OrderedDict(sorted(db.items(), key=lambda t: t[0]))
for key in db:
    print(key)
Reply


Messages In This Thread
Easy way to sort a nested dict - by Alfalfa - Dec-07-2018, 04:21 AM
RE: Easy way to sort a nested dict - by buran - Dec-07-2018, 08:34 AM
RE: Easy way to sort a nested dict - by Gribouillis - Dec-07-2018, 10:07 AM
RE: Easy way to sort a nested dict - by Alfalfa - Dec-07-2018, 04:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,328 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,295 Feb-09-2022, 09:37 AM
Last Post: tbaror
  changing key names in nested dict wardancer84 6 2,161 Sep-10-2021, 08:13 AM
Last Post: wardancer84
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,908 Jan-22-2021, 05:43 AM
Last Post: buran
  [nested dics] Easy way to find if a key exists? Winfried 9 2,995 Sep-17-2020, 05:30 PM
Last Post: deanhystad
  Sort a dict in dict cherry_cherry 4 76,210 Apr-08-2020, 12:25 PM
Last Post: perfringo
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,219 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  How can I sort my dict ? Mike Ru 1 1,817 Feb-16-2019, 11:50 PM
Last Post: ichabod801
  how to create a nested dict.. wardancer84 5 3,678 Nov-23-2018, 04:01 AM
Last Post: Larz60+
  sorting nested dict according to values merlem 6 17,642 Apr-01-2017, 10:01 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020