skip to Main Content

how to sort a dict list into a single list with many lists inside it and within each single list all the sorted dicts?

Example

[
  {
    "Day": "Giornata 29",
    "Matches": {
      "Home": "Egnatia",
      "Away": "Kukesi",
      "Times": "19.03. 15:00",
      "Championship": "CALCIOnALBANIA Super Leaguen2023/2024"
    }
  },
  {
    "Day": "Giornata 29",
    "Matches": {
      "Home": "Egnatia",
      "Away": "Kukesi",
      "Times": "20.03. 16:09",
      "Championship": "CALCIOnALBANIA Super Leaguen2023/2024"
    }
  },
  {
    "Day": "Giornata 41",
    "Matches": {
      "Home": "Lincoln",
      "Away": "Leyton Orient",
      "Times": "19.03. 16:00",
      "Championship": "CALCIOnINGHILTERRA League Onen2023/2024"
    }
  },
  {
    "Day": "Giornata 30",
    "Matches": {
      "Home": "Napoli",
      "Away": "Atalanta",
      "Times": "30.03. 12:30",
      "Championship": "CALCIOnITALIA Serie An2023/2024"
    }
  }
]

I expect an output like this:

{
  "Giornata 29": {
    "CALCIOnALBANIA Super Leaguen2023/2024": [
      {
        "Day": "Giornata 29",
        "Matches": {
          "Home": "Egnatia",
          "Away": "Kukesi",
          "Times": "19.03. 15:00",
          "Championship": "CALCIOnALBANIA Super Leaguen2023/2024"
        }
      },
      {
        "Day": "Giornata 29",
        "Matches": {
          "Home": "Egnatia",
          "Away": "Kukesi",
          "Times": "19.03. 15:00",
          "Championship": "CALCIOnALBANIA Super Leaguen2023/2024"
        }
      }
    ]
  },
  "Giornata 41": {
    "CALCIOnINGHILTERRA League Onen2023/2024": [
      {
        "Day": "Giornata 41",
        "Matches": {
          "Home": "Lincoln",
          "Away": "Leyton Orient",
          "Times": "19.03. 16:00",
          "Championship": "CALCIOnINGHILTERRA League Onen2023/2024"
        }
      }
    ]
  },
  "Giornata 30": {
    "CALCIOnITALIA Serie An2023/2024": [
      {
        "Day": "Giornata 30",
        "Matches": {
          "Home": "Napoli",
          "Away": "Atalanta",
          "Times": "30.03. 12:30",
          "Championship": "CALCIOnITALIA Serie An2023/2024"
        }
      }
    ]
  }
}

I expect many lists sorted by "Day" and "Championship", how can this be done in the simplest way possible?

2

Answers


  1. Assuming that you input data is in a variable named data, you could do this (with the caveat noted in my comment on your question — this produces the sample output you show, not what you describe in the beginning of the question):

    from collections import defaultdict
    
    data = ...
    
    sorted_data = defaultdict(list)
    for item in data:
        sorted_data[item["Day"]].append(item)
    

    If you want the inner list sorted by ...["Matches"]["Championship"], you could do this:

    from collections import defaultdict
    from bisect import insort
    
    
    sorted_data = defaultdict(list)
    for item in data::
        insort(sorted_data[item["Day"]], item, key=lambda x: x["Matches"]["Championship"])
    
    Login or Signup to reply.
  2. If you want the results in a sorted manner, then you might want to sort your input data as a first step. Since dictionaries preserve insertion order you should be able to preserve your sort order when constructing you result.

    I personally prefer setdefault() to defaultdict() but they very similar things in the end.

    You can read more about dict.setdefault()

    data_in = [
        {"Day": "Giornata 29", "Matches": {"Home": "Egnatia", "Away": "Kukesi", "Times": "19.03. 15:00", "Championship": "CALCIOnALBANIA Super Leaguen2023/2024"}},
        {"Day": "Giornata 29", "Matches": {"Home": "Egnatia", "Away": "Kukesi", "Times": "20.03. 16:09", "Championship": "CALCIOnALBANIA Super Leaguen2023/2024"}},
        {"Day": "Giornata 41", "Matches": {"Home": "Lincoln", "Away": "Leyton Orient", "Times": "19.03. 16:00", "Championship": "CALCIOnINGHILTERRA League Onen2023/2024"}},
        {"Day": "Giornata 30", "Matches": {"Home": "Napoli", "Away": "Atalanta", "Times": "30.03. 12:30", "Championship": "CALCIOnITALIA Serie An2023/2024"}}
    ]
    
    ## ---------------------
    ## pre-sort the rows based on day|chamionship|time
    ## sort() orders the rows in place updating data_in as it does so.
    ## in this case, the order is specified by the tuple (Day, Championship, Time)
    ## ---------------------
    data_in.sort(key=lambda row: (row["Day"], row["Matches"]["Championship"], row["Matches"]["Times"]))
    ## ---------------------
    
    data_out = {}
    for item in data_in:
        matches = item["Matches"]
    
        ## ---------------------
        ## value = some_dictionary.setdefult(key, default_value)
        ## Will set some_dictionary[key] = default_value if key is not in some_dictionary.
        ## it will set value to some_dictionary[key]
        ## ---------------------
        data_out 
            .setdefault(item["Day"], {}) 
            .setdefault(matches["Championship"], []) 
            .append(dict(
                (k,v)
                for k,v
                in matches.items()
                if k in ["Home", "Away", "Times"]
            ))
    
        ## -----------------
        ## An equivalent version of the above that might be easier
        ## to follow is:
        ## -----------------
        """
        target = data_out
        target = target.setdefault(item["Day"], {})
        target = target.setdefault(matches["Championship"], [])
        target.append({
            "Home": matches["Home"],
            "Away": matches["Away"],
            "Times": matches["Times"],
        })
        """
        ## -----------------
    
    import json
    print(json.dumps(data_out, indent=4))
    

    Giving you:

    {
        "Giornata 29": {
            "CALCIOnALBANIA Super Leaguen2023/2024": [
                {
                    "Home": "Egnatia",
                    "Away": "Kukesi",
                    "Times": "19.03. 15:00"
                },
                {
                    "Home": "Egnatia",
                    "Away": "Kukesi",
                    "Times": "20.03. 16:09"
                }
            ]
        },
        "Giornata 30": {
            "CALCIOnITALIA Serie An2023/2024": [
                {
                    "Home": "Napoli",
                    "Away": "Atalanta",
                    "Times": "30.03. 12:30"
                }
            ]
        },
        "Giornata 41": {
            "CALCIOnINGHILTERRA League Onen2023/2024": [
                {
                    "Home": "Lincoln",
                    "Away": "Leyton Orient",
                    "Times": "19.03. 16:00"
                }
            ]
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search