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
  Why changing data in a copied list changes the original list? plumberpy 3 365 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Order a list with successive permutations based on another list yvrob 3 751 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Saving list in a list quest_ 3 897 Mar-10-2021, 09:58 AM
Last Post: quest_
Star Convert Bytearray into List using list() Shlok 2 656 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  Adding List Element if Second part of the List Elements are the Same quest_ 3 751 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Count number of occurrences of list items in list of tuples t4keheart 1 722 Nov-03-2020, 05:37 AM
Last Post: deanhystad
Question Save list with nested list into CSV SpongeB0B 1 1,402 Oct-12-2020, 07:26 AM
Last Post: bowlofred
  Appending to list of list in For loop nico_mnbl 2 677 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  How to make a list of values from a dictionary list? faryad13 2 742 Sep-03-2020, 03:45 PM
Last Post: faryad13
  Append list into list within a for loop rama27 2 867 Jul-21-2020, 04:49 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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