Python Forum
Compile list of dictianories out of another list of dictianories by certain keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compile list of dictianories out of another list of dictianories by certain keys
#1
Hi everyone,

I need to compile a new list of new dictianories out of another list of dictianories. The new list should contain dictianories from the old one, reduced to pre determined keys if the match regarding another predetermined list of keys and values.

It can be assumed that all dictianories have exactly the same keys. The list cointains of thousands of individual dictianories.

What I have in mind is something like:
lod = [{"id": 1, "a": 1, "b": 2, "result": 9.82}, {"id": 2, "a": 1, "b": 2, "result": -5}, {"id": 3, "a": 1, "b": 5, "result": 7.98}]

newList = selectionFunction(lod, ["a", "b"], [1, 2], ["id", "result"])
# Return should be: [{"id": 1, "result": 9.82}, {"id": 2, "result": -5}]
How can I code this efficiently? I have a function but it takes forever.

Thanks in advance
Markus
Reply


Messages In This Thread
Compile list of dictianories out of another list of dictianories by certain keys - by CatorCanulis - Jun-05-2021, 02:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,161 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,513 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 915 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,833 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,557 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,279 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Updating nested dict list keys tbaror 2 1,276 Feb-09-2022, 09:37 AM
Last Post: tbaror
  How to check if a list is in another list finndude 4 1,836 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 4,026 Jan-14-2022, 09:30 PM
Last Post: snippsat
  Remove empty keys in a python list python_student 7 3,018 Jan-12-2022, 10:23 PM
Last Post: python_student

Forum Jump:

User Panel Messages

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