Skip to main content
https://img.shields.io/pypi/v/pytups.svg https://img.shields.io/pypi/l/pytups.svg https://img.shields.io/pypi/pyversions/pytups.svg

What and why

The idea is to allow sparse operations to be executed in matrix data.

I grew used to the chained operations in R’s tidyverse packages or, although not a great fan myself, python’s pandas . I find myself using dictionary and list comprehensions all the time to pass from one data format to the other efficiently. But after doing it for the Nth time, I thought of automaticing it.

In my case, it helps me construct optimisation models with PuLP. I see other possible uses not related to OR.

I’ve implemented some additional methods to regular dictionaries, lists and sets to come up with interesting methods that somewhat quickly pass from one to the other and help with data wrangling.

In order for the operations to make any sense, the assumption that is done is that whatever you are using has the same ‘structure’. For example, if you a have a list of tuples: every element of the list is a tuple with the same size and the Nth element of the tuple has the same type, e.g. [(1, 'red', 'b', '2018-01'), (10, 'ccc', 'ttt', 'ff')]. Note that both tuples have four elements and the first one is a number, not a string. We do not check that this is consistent.

They’re made to always return a new object, so no “in-place” editing, hopefully.

Right now there are three classes to use: dictionaries, tuple lists and ordered sets.

Python versions

Python 3.9 and up.

Quick example

We index a tuple list according to some index positions.:

import pytups as pt
some_list_of_tuples = [('a', 'b', 'c', 1), ('a', 'b', 'c', 2), ('a', 'b', 'c', 45)]
tp_list = pt.TupList(some_list_of_tuples)
tp_list.to_dict(result_col=3)
# {('a', 'b', 'c'): [1, 2, 45]}
tp_list.to_dict(result_col=3).to_dictdict()
# {'a': {'b': {'c': [1, 2, 45]}}}
tp_list.to_dict(result_col=[2, 3])
# {('a', 'b'): [('c', 1), ('c', 2), ('c', 45)]}

We do some operations on dictionaries with common keys.:

import pytups as pt
some_dict = pt.SuperDict(a=1, b=2, c=3, d=5)
some_other_dict = pt.SuperDict(a=5, b=7, c=1)
some_other_dict + some_dict
# {'a': 6, 'b': 9, 'c': 4}
some_other_dict.vapply(lambda v: v**2)
# {'a': 25, 'b': 49, 'c': 1}
some_other_dict.kvapply(lambda k, v: v/some_dict[k])
# {'a': 5.0, 'b': 3.5, 'c': 0.3333333333333333}

Installing

pip install pytups

or, for the development version:

pip install https://github.com/pchtsp/pytups/archive/master.zip

Testing

Run the command:

python -m unittest discover -s tests

if the output says OK, all tests were passed.

Type checking

Install the development dependencies (including ty) with uv, then run the type checker from the project root:

uv sync --group dev
uv run ty check

If ty check finishes without reporting errors, the type check passed.

Metadata

Release files for pytups 1.0.5

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

Source distribution (sdist)

Source distribution for pytups 1.0.5
File Size Uploaded
pytups-1.0.5.tar.gz 20.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytups 1.0.5
File Interpreter ABI Platform
pytups-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 35.2 kB

Release files / pytups-1.0.5.tar.gz

Download URL pytups-1.0.5.tar.gz
Size 20.2 kB
Tags Source
SHA-256 checksum
How to use checksums
aa924784763e7c46044d3672ed1b460ce41677e64238c83a54f61271eecd7df3
BLAKE2b-256 checksum
How to use checksums
886313f43e12f8de70de9c4ba3e00cb8843a9c5507851f3e40eb58740fb061d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / pytups-1.0.5-py3-none-any.whl

Download URL pytups-1.0.5-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90ea657c23a6a28e2b3cdb9b9198ab86da8cba3fe230f77722f53ed3c7d4be53
BLAKE2b-256 checksum
How to use checksums
b6a081c25b2e5e19259b5637101d998a9bb63d77fdf44b6a689a4bfd2d5652a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.0

2 release files

0.88.1

2 release files

0.87.3

2 release files

0.87.1

2 release files

0.86.2

2 release files

0.86.1

2 release files

0.85.0

2 release files

0.84.0

2 release files

0.83.0

2 release files

0.82.0

2 release files

0.80.0

2 release files

0.9.0

2 release files

0.7

2 release files

0.6

2 release files

0.5

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

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