Skip to main content

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

Project description

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'},)}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

group-by-attr-1.0.1.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

group_by_attr-1.0.1-py2.py3-none-any.whl (2.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file group-by-attr-1.0.1.tar.gz.

File metadata

File hashes

Hashes for group-by-attr-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ad618711b9817ae37f7abd2073749834fd15b9ac133b92939566be2ca0048d0f
MD5 a5780b7777cccd6f14e904e973f745d2
BLAKE2b-256 cba5f5bfd3ae4b929345f2c33c9ed777a77cb5f7299ebad0abc387fb49eab3af

See more details on using hashes here.

File details

Details for the file group_by_attr-1.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for group_by_attr-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5e4c47ca34e3f1cfe233fd1a1366303693a1315e33503395495db63d2ba1228e
MD5 2134c1d659fea4935764398700723bb2
BLAKE2b-256 0da25b7fdd6673d045c97bf764294dcca0cf2149bc6f0ba2add69ce25c80c00b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page