Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ordering a list of dict
#1
Hello everybody,

I have a list of dictionaries i need to perform some actions on. Following the sample code:

j = json.loads(value)
#list of dict is unordered here
self.Action(j)
#list of dict is unordered here
In "self.Action" i use the following code to order the list and to assign a new field to all dictionaries:

def Action(self, pattern)
pattern = sorted(pattern, key=lambda k: k['order'])
#list of dict is ordered here
for p in pattern:
    p["new_val"] = p["order"]
When i order the list in "self.Action" it actually works out, if i print the list of dictionaries i have the right order and i have the new field ["new_val"].
The weird thing is that just after the self.Action method, the dictionaries keep the new field but they are no longer ordered in the list..
How is that possible?

Thanks
Reply
#2
pattern is local variable for def Action. From the code provided you don't return the sorted list, so you don't change anything outside the method. However because lists are mutable, they preserve the new element added. You can use the mutability also for sorting and instead of pattern = sorted(pattern, key=lambda k: k['order']) use pattern.sort(key=lambda k: k['order'])

my_list = [{'foo':'a', 'order':3}, {'foo':'b', 'order':1}, {'foo':'c', 'order':2}]

def action(some_list):
    some_list.sort(key=lambda x: x['order'])
    for item in some_list:
        item['bar'] = None
    
action(my_list)
print(my_list)
Output:
[{'foo': 'b', 'bar': None, 'order': 1}, {'foo': 'c', 'bar': None, 'order': 2}, { 'foo': 'a', 'bar': None, 'order': 3}] >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hello,

Thanks a lot, that worked. And thanks for the nice explanation :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,161 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,413 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  drf ordering by custom field and add ranking in to_representation tomfong521 0 1,828 Mar-24-2021, 09:56 AM
Last Post: tomfong521
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,256 Jan-30-2021, 07:11 AM
Last Post: alloydog
  Help with Matplotlib and ordering the axis theliberalguy97 3 7,748 Dec-12-2020, 08:06 PM
Last Post: deanhystad
Question dict value, how to change type from int to list? swissjoker 3 2,692 Dec-09-2020, 09:50 AM
Last Post: perfringo
  What are these ordering code doing coltson 1 1,761 Nov-03-2020, 05:07 AM
Last Post: deanhystad
  How to remove dict from a list? Denial 7 2,851 Sep-28-2020, 02:40 PM
Last Post: perfringo
  Trouble with converting list , dict to int values! faryad13 7 3,671 Sep-04-2020, 06:25 AM
Last Post: faryad13

Forum Jump:

User Panel Messages

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