Skip to main content

Mojo function

drop_while

drop_while[origin: ImmutOrigin, IterableType: Iterable, //, predicate: fn(IterableType.IteratorType[False, origin].Element) -> Bool](ref[origin] iterable: IterableType) -> _DropWhileIterator[predicate]

Creates an iterator that drops elements while predicate returns True.

This function returns an iterator that drops elements from the input iterable as long as the predicate function returns True for each element. Once the predicate returns False, the iterator starts yielding all remaining elements unconditionally.

Examples:

from itertools import drop_while

# Drop while less than 5
fn less_than_5(x: Int) -> Bool:
    return x < 5

var nums = [1, 2, 3, 4, 5, 6, 1, 2]
for num in drop_while[less_than_5](nums):
    print(num)  # Prints: 5, 6, 1, 2

Parameters:

  • ​origin (ImmutOrigin): The origin of the iterable.
  • ​IterableType (Iterable): The type of the iterable.
  • ​predicate (fn(IterableType.IteratorType[False, origin].Element) -> Bool): A function that takes an element and returns True if the element should be dropped.

Args:

  • ​iterable (IterableType): The iterable to drop elements from.

Returns:

_DropWhileIterator: An iterator that drops elements while predicate returns True, then yields all remaining elements.

Was this page helpful?