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
#11
(Jun-06-2021, 03:49 PM)Caprone Wrote: this is a good solution but not a pythonic solution;
You should always try to avoid nested loop where possible and take advantage of list/dict comprehension;
and more --> try to simplify the problem -->
in this case You only need to know if kwargs' dict is a subset of the 'row' dict (as constraint made by OT) , no checking loop needed:

I agree that flat is better than nested. And I like subset idea.

But regarding 'pythonic'.... if we look at the the code provided then generator expression has also nested for-loop (albeit on one row) and character count is not in line with 'Limit all lines to a maximum of 79 characters.'. So, how should one judge this code from perspective of being 'pythonic'... Wink.

Applying 'pythonic' convention to tackle long line ('Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent'):

def filter_dicts(data, *args, **kwargs):
    return  ({key: row[key] for key in args}
              for row in data
              if row.items() >= kwargs.items())
and as OP already mentioned, these codes are not equal as give different results with different datasets. You know, all that stuff regarding errors passed silently.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


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,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 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,738 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,501 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,201 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  How to check if a list is in another list finndude 4 1,794 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 3,966 Jan-14-2022, 09:30 PM
Last Post: snippsat
  Remove empty keys in a python list python_student 7 2,903 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