sorted_chain
itertools.chain with sorted result.
This is a simple function. However, it is nice to have it tested and the edge cases taken care of. If you encounter any problem with this or like to extend it, please open an Issue or Pull Request!
Installation
You can install sorted_chain from PyPI:
pip install sorted_chain
Usage
You can pass any amount of iterators of sorted values to sorted_chain().
The result will be sorted, then.
>>> from sorted_chain import sorted_chain, IterableIsNotSorted
>>> list(sorted_chain([1, 3, 5], [2, 4, 6]))
[1, 2, 3, 4, 5, 6]
The input iterables must be sorted! Otherwise you will receive a IterableIsNotSorted exception.
>>> from sorted_chain import IterableIsNotSorted
>>> try:
... list(sorted_chain([1000, 0])) # wrong order
... except IterableIsNotSorted:
... print("error!")
error!
Iterators can also be in decending order:
>>> list(sorted_chain([100, 10, 1], [99, 9, -1], reverse=True))
[100, 99, 10, 9, 1, -1]
The input can be generators. Their values are only retrieved when needed.
>>> large_generator1 = iter(range(1000))
>>> large_generator2 = iter(range(1000))
>>> for value in sorted_chain(large_generator1, large_generator2):
... if value >= 100:
... break
>>> next(large_generator1) # this generator had reached 100
101
>>> next(large_generator2) # this one yielded 99 and 100, too
101
If you have elements that you would like to sort with a key, you can do that as expected:
>>> positive = [1, 3, 5]
>>> negative = [-2, -4, -6]
>>> list(sorted_chain(positive, negative, key=lambda x: x*x)) # sort without minus
[1, -2, 3, -4, 5, -6]
Changelog
-
v0.0.2
- Correct metadata
- Correct license
-
v0.0.1
- Initial release
New Releases
For a new release:
- Edit the changelog section
- Commit the changes
- Let the GitHub Actions tests run
- Create a tag and push it:
git tag v0.0.1
git push origin v0.0.1
Release files for sorted-chain 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sorted_chain-0.0.2.tar.gz | 35.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sorted_chain-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 69.6 kB
Release files / sorted_chain-0.0.2.tar.gz
| Download URL | sorted_chain-0.0.2.tar.gz |
|---|---|
| Size | 35.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7859c355badff3e0be0e6595e2025fc128ce2e7263168f7197a2ea04bb814052
|
|
BLAKE2b-256 checksum How to use checksums |
1121352d4f9bdf600e837b9e17e2f277f4bdec4d47dc11115f7ac6d40784cd2f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.5
|
Release files / sorted_chain-0.0.2-py3-none-any.whl
| Download URL | sorted_chain-0.0.2-py3-none-any.whl |
|---|---|
| Size | 34.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c79b22425b6e38f1e2e4cf48a08e0b16d1f28d0feaf4843dc04488a01ed00759
|
|
BLAKE2b-256 checksum How to use checksums |
197d8a009b6f823281db1b0f6210ba764fea074bc5a68238571039383f7a49a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.5
|