Skip to content

Efficiently get the index-0 element of an iterable.

License

Notifications You must be signed in to change notification settings

GalacticDynamics/zeroth

Repository files navigation

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

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.

About

Efficiently get the index-0 element of an iterable.

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •  

Languages