Skip to main content

group-by-attr Version Build Coverage Health

Compatibility Implementations Format Downloads

Group items in a sequence by the value of a shared attribute.

group_by_attr(attr, items)

Installation:

$ pip install group-by-attr

Example

Let’s say you want to group some “Struct” instances together.

>>> from pprint import pprint
>>> from collections import namedtuple
>>> Struct = namedtuple('Struct', ('x', 'y', 'z'))
>>> a, b, c = (
...     Struct(x=1, y=1, z=1),
...     Struct(x=1, y=2, z=2),
...     Struct(x=1, y=1, z=3))

If we were to group these instances by the ‘x’ attribute, we should expect a single group containing all three items:

>>> pprint(group_by_attr(attr='x', items=(a, b, c)))
{1: (Struct(x=1, y=1, z=1),
     Struct(x=1, y=2, z=2),
     Struct(x=1, y=1, z=3))}

If, instead, we were to group by ‘y’, we should expect a different grouping:

>>> pprint(group_by_attr(attr='y', items=(a, b, c)))
{1: (Struct(x=1, y=1, z=1),
     Struct(x=1, y=1, z=3)),
 2: (Struct(x=1, y=2, z=2),)}

Finally, grouping by ‘z’ will result in three separate groups:

>>> pprint(group_by_attr(attr='z', items=(a, b, c)))
{1: (Struct(x=1, y=1, z=1),),
 2: (Struct(x=1, y=2, z=2),),
 3: (Struct(x=1, y=1, z=3),)}

This function can also use an alternate getattr, as long as it implements the same interface (taking an item and an attribute name as arguments). For example, you could group dictionaries:

>>> pprint(group_by_attr(
...     attr='x',
...     items=(
...         {'x': 1, 'y': 'a'},
...         {'x': 2, 'y': 'b'},
...         {'x': 1, 'y': 'c'}),
...     getattr_fn=dict.__getitem__))
{1: ({'x': 1, 'y': 'a'},
     {'x': 1, 'y': 'c'}),
 2: ({'x': 2, 'y': 'b'},)}

Metadata

Release files for group-by-attr 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for group-by-attr 1.0.1
File Size Uploaded Provenance Actions
group-by-attr-1.0.1.tar.gz 2.1 kB Without provenance Details for group-by-attr-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for group-by-attr 1.0.1
File Interpreter ABI Platform Actions
group_by_attr-1.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details for group_by_attr-1.0.1-py2.py3-none-any.whl

Total release size: 5.1 kB

Release files / group-by-attr-1.0.1.tar.gz

Download URL group-by-attr-1.0.1.tar.gz
Size 2.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ad618711b9817ae37f7abd2073749834fd15b9ac133b92939566be2ca0048d0f
BLAKE2b-256 checksum
How to use checksums
cba5f5bfd3ae4b929345f2c33c9ed777a77cb5f7299ebad0abc387fb49eab3af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / group_by_attr-1.0.1-py2.py3-none-any.whl

Download URL group_by_attr-1.0.1-py2.py3-none-any.whl
Size 2.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5e4c47ca34e3f1cfe233fd1a1366303693a1315e33503395495db63d2ba1228e
BLAKE2b-256 checksum
How to use checksums
0da25b7fdd6673d045c97bf764294dcca0cf2149bc6f0ba2add69ce25c80c00b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for group-by-attr 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page