https://github.com/galacticdynamics/zeroth
Efficiently get the index-0 element of an iterable.
https://github.com/galacticdynamics/zeroth
Last synced: 5 months ago
JSON representation
Efficiently get the index-0 element of an iterable.
- Host: GitHub
- URL: https://github.com/galacticdynamics/zeroth
- Owner: GalacticDynamics
- License: mit
- Created: 2024-07-31T15:23:37.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-01T21:55:29.000Z (about 1 year ago)
- Last Synced: 2025-05-01T22:33:10.593Z (about 1 year ago)
- Language: Python
- Homepage:
- Size: 33.2 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
zeroth
Efficiently get the index-0 element of an iterable.
This is a micro-package, containing the single function `zeroth`. `zeroth`
is syntactic sugar for `next(iter(obj))`, with a nice docstring.
## Installation
[![PyPI platforms][pypi-platforms]][pypi-link]
[![PyPI version][pypi-version]][pypi-link]
```bash
pip install zeroth
```
## Documentation
[![Actions Status][actions-badge]][actions-link]
```python
from zeroth import zeroth
print(zeroth([0, 1, 2]))
# 0
print(zeroth((3, 2, 1)))
# 3
print(zeroth({"a": 1, "b": 2, "c": 3}))
# 'a'
print(zeroth(range(3)))
# 0
print(zeroth(range(1, 3)))
# 1
print(zeroth(map(str, range(3))))
# '0'
import numpy as np
print(zeroth(np.array([1, 2, 3])))
# 1
class ReverseIterable:
def __init__(self, data):
self.data = data
def __iter__(self):
return iter(reversed(self.data))
print(zeroth(ReverseIterable([1, 2, 3])))
# 3
```
## Q&A
_Why isn't this called `first` since it gets the element with ordinality of 1?_
Because that package name is already taken on PyPI. Here zeroth refers to the
index.
[actions-badge]: https://github.com/GalacticDynamics/zeroth/workflows/CI/badge.svg
[actions-link]: https://github.com/GalacticDynamics/zeroth/actions
[conda-badge]: https://img.shields.io/conda/vn/conda-forge/zeroth
[conda-link]: https://github.com/conda-forge/zeroth-feedstock
[github-discussions-badge]: https://img.shields.io/static/v1?label=Discussions&message=Ask&color=blue&logo=github
[github-discussions-link]: https://github.com/GalacticDynamics/zeroth/discussions
[pypi-link]: https://pypi.org/project/zeroth/
[pypi-platforms]: https://img.shields.io/pypi/pyversions/zeroth
[pypi-version]: https://img.shields.io/pypi/v/zeroth