Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
group by property
#22
My experience is that if more complexity is introduced to data structure then it is good time to return to the drawing board and rethink your approach.

Why there should be additional complexity? Why this structure is required? Source data is list of dictionaries and result is also list of (more complicated) dictionaries. In order to filter out something from this list you have to iterate anyway. Isin't it possible that it is easier to iterate original list instead of creating more complex structure and iterate over it?

Want to have dictionaries from list which are 'good', where n = 1 and without 'm'? List comprehension should be enough:

>>> orig = [{'name': 'John', 'm': 'good', 'n': 1},
...         {'name': 'Alina','m': 'good', 'n': 1},
...         {'name': 'Olivia', 'm': 'bad', 'n': 2},
...         {'name': 'Ruby', 'm': 'bad','n': 2}]
>>> [{k: v for k, v in row.items() if k != 'm'} for row in orig if row['n'] == 1 and row['m'] == 'good']
[{'name': 'John', 'n': 1}, {'name': 'Alina', 'n': 1}]
For me it's strange to name source data as 'result'. It may create confusion and confusion may lead to buggy code.
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


Messages In This Thread
group by property - by bhojendra - Jun-30-2019, 05:08 AM
RE: group by property - by SheeppOSU - Jun-30-2019, 05:16 AM
RE: group by property - by bhojendra - Jun-30-2019, 05:35 AM
RE: group by property - by SheeppOSU - Jun-30-2019, 06:06 AM
RE: group by property - by bhojendra - Jun-30-2019, 06:19 AM
RE: group by property - by SheeppOSU - Jun-30-2019, 06:27 AM
RE: group by property - by scidam - Jun-30-2019, 12:00 PM
RE: group by property - by bhojendra - Jun-30-2019, 12:25 PM
RE: group by property - by SheeppOSU - Jun-30-2019, 12:45 PM
RE: group by property - by bhojendra - Jun-30-2019, 01:12 PM
RE: group by property - by SheeppOSU - Jun-30-2019, 01:29 PM
RE: group by property - by bhojendra - Jun-30-2019, 01:32 PM
RE: group by property - by SheeppOSU - Jun-30-2019, 01:51 PM
RE: group by property - by bhojendra - Jun-30-2019, 01:54 PM
RE: group by property - by SheeppOSU - Jun-30-2019, 02:03 PM
RE: group by property - by bhojendra - Jun-30-2019, 02:16 PM
RE: group by property - by SheeppOSU - Jun-30-2019, 02:25 PM
RE: group by property - by bhojendra - Jun-30-2019, 03:00 PM
RE: group by property - by SheeppOSU - Jun-30-2019, 03:14 PM
RE: group by property - by bhojendra - Jun-30-2019, 03:32 PM
RE: group by property - by SheeppOSU - Jun-30-2019, 03:55 PM
RE: group by property - by perfringo - Jul-01-2019, 07:26 AM
RE: group by property - by bhojendra - Jul-01-2019, 08:23 AM
RE: group by property - by perfringo - Jul-01-2019, 11:04 AM
RE: group by property - by bhojendra - Jul-01-2019, 06:43 PM
RE: group by property - by perfringo - Jul-01-2019, 08:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 4,030 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 9,267 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 5,573 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 4,399 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Property price calculation oli_action 4 4,690 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Use of @property decorator ruy 16 10,911 Jun-09-2020, 05:29 PM
Last Post: buran
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,742 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 3,208 Nov-25-2019, 04:32 PM
Last Post: KaliLinux
  print all method and property of list object engmoh 4 4,023 Oct-26-2019, 05:33 PM
Last Post: engmoh
  what is the doc parameter of property? microphone_head 2 3,444 Apr-11-2019, 08:59 AM
Last Post: microphone_head

Forum Jump:

User Panel Messages

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