Skip to content

Latest commit

 

History

History
74 lines (50 loc) · 1.79 KB

README.md

File metadata and controls

74 lines (50 loc) · 1.79 KB

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 version

pip install zeroth

Documentation

Actions Status

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