Skip to content

Commit

Permalink
Add a function loop_in_pairs to ops.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeqfu committed Nov 9, 2021
1 parent 4d26dbc commit cc26add
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pyhelpers/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,34 @@ def get_number_of_chunks(file_or_obj, chunk_size_limit=50, binary=True):

# Iterable

def loop_in_pairs(iterable):
"""
A function to iterate a list as pair (current, next).
:param iterable: iterable object
:type iterable: typing.Iterable
:return: a `zip <https://docs.python.org/3.9/library/functions.html#zip>`_-type variable
:rtype: zip
**Tests**::
>>> from pyhelpers.ops import loop_in_pairs
>>> res = loop_in_pairs(iterable=[1])
>>> list(res)
[]
>>> res = loop_in_pairs(iterable=[1, 2])
>>> list(res)
[(1, 2)]
"""

a, b = itertools.tee(iterable)
next(b, None)

return zip(a, b)


def split_list_by_size(lst, sub_len):
"""
Split a list into (evenly sized) sub-lists.
Expand Down

0 comments on commit cc26add

Please sign in to comment.