Python Forum
sort list of dictionaries by value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sort list of dictionaries by value
#1
i am trying to sort a list of dictionaries based by a value of a specific key under the column of name values. i found two ways to do this
first i found


new_lst=sorted(lst,key=itemgetter(name))
this gives me an error code of

Error:
TypeError: unhashable type: 'list'
i have also tried



new_lst=sorted(lst,key=lambda d: d["name"])
the latter one gives me an error of


Error:
TypeError: list indices must be integers or slices, not str
i have scoured the net and i cant understand why these methods dont work.... please help
Reply
#2
In this:
new_lst=sorted(lst,key=lambda d: d["name"])
d must not be a dictionary. What is in lst? It isn't dictionaries

In this code I create a list of dictionaries containing information about files in my folder. I then sort by is_dir, extension and finally filename.
from pathlib import Path

files = [{"name": x.name, "ext": x.suffix, "isdir": x.is_dir()} for x in Path(".").iterdir()]

print(*sorted(files, key=lambda x: (x["isdir"], x["ext"], x["name"])), sep="\n")
Output:
{'name': 'data.csv', 'ext': '.csv', 'isdir': False} {'name': 'input.csv', 'ext': '.csv', 'isdir': False} {'name': 'test.csv', 'ext': '.csv', 'isdir': False} {'name': 'interactiveconsole.py', 'ext': '.py', 'isdir': False} {'name': 'test_dir_sort.py', 'ext': '.py', 'isdir': False} {'name': 'test.txt', 'ext': '.txt', 'isdir': False} {'name': 'data.xlsx', 'ext': '.xlsx', 'isdir': False} {'name': '.vscode', 'ext': '', 'isdir': True} {'name': '__pycache__', 'ext': '', 'isdir': True} {'name': 'fastapi_demo', 'ext': '', 'isdir': True} {'name': 'games', 'ext': '', 'isdir': True} {'name': 'venv', 'ext': '', 'isdir': True}
itemgetter also works fine. Here I sort by name.
from pathlib import Path
from operator import itemgetter

files = [{"name": x.name, "ext": x.suffix, "isdir": x.is_dir()} for x in Path(".").iterdir()]
print(*sorted(files, key=itemgetter("name")), sep="\n")
From the error messages I think lst is a list of lists or tuples.
from pathlib import Path
from operator import itemgetter

files = [(x.name, x.suffix, x.is_dir) for x in Path(".").iterdir()]
print(*sorted(files, key=itemgetter("name")), sep="\n")
Error:
Traceback (most recent call last): File ...", line 5, in <module> print(*sorted(files, key=itemgetter("name")), sep="\n") TypeError: tuple indices must be integers or slices, not str
Reply
#3
Change
new_lst = sorted(lst,key=itemgetter(name))
to

new_lst = sorted(lst,key=itemgetter("name"))
Example:


from operator import itemgetter

# somewhere in your code is a list assigned to name
name = []

# example of list of dicts with content
dicts = [{"value": x, "name": f"name{x}"} for x in range(1, 11)]

# somewhere else you are accessing the name, which is a list
# if name were not defined, you'll get instead an NameError

# but here you'll get an TypeError, because a dict can not have a mutable key
# and name is a list
# sorted_dicts = sorted(dicts, key=itemgetter(name))

# this is right
sorted_dicts = sorted(dicts, key=itemgetter("name"))

print("Before")
print(dicts)
print()

print("After")
print(sorted_dicts)
Additional task: Try to understand, why {'value': 10, 'name': 'name10'} is the second element in the list after the sorting.
Pedroski55 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Here is the correct usage using itemgetter and lambda.

Using itemgetter:
from operator import itemgetter

lst = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30},
    {"name": "Charlie", "age": 20}
]

new_lst = sorted(lst, key=itemgetter("name"))
print(new_lst)
Output:
Output:
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 20}]
Using lambda:
lst = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30},
    {"name": "Charlie", "age": 20}
]

new_lst = sorted(lst, key=lambda d: d["name"])
print(new_lst)
Output:
Output:
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 20}]
Reply
#5
In this example:
new_lst=sorted(lst,key=itemgetter(name))
Error:
Error: TypeError: unhashable type: 'list'
The error message tells ups that name is a list. You should use a key name, like "name", but that is not going to fix your problem.

In this example:
new_lst=sorted(lst,key=lambda d: d["name"])
Error:
Error: TypeError: list indices must be integers or slices, not str
The error mesage tells us that d, one of the things in lst, is a list. lst is not a list of dictionaries, it is a list of lists. Fix that problem first.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 804 Apr-29-2024, 04:38 PM
Last Post: Calab
  list.sort() returning None SmallCoder14 8 928 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  Access list of dictionaries britesc 4 1,322 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,443 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,433 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  [solved] Sort list paul18fr 5 3,061 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  function that returns a list of dictionaries nostradamus64 2 1,920 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Sort List of Lists by Column Nju 1 15,088 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  convert List with dictionaries to a single dictionary iamaghost 3 3,051 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to sort os.walk list? Denial 6 12,215 Oct-10-2020, 05:28 AM
Last Post: Denial

Forum Jump:

User Panel Messages

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