IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0b2
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

take

def take[origin: ImmutOrigin, IterableType: Iterable, //](ref[origin] iterable: IterableType, count: Int) -> _TakeIterator[IterableType.IteratorType[False, origin._mlir_origin, origin]]

Creates an iterator that yields the first count elements.

This function returns an iterator that yields at most count elements from the input iterable, then stops.

Examples:

from std.itertools import take

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

Parameters:

  • origin (ImmutOrigin): The origin of the iterable.
  • IterableType (Iterable): The type of the iterable.

Args:

  • iterable (IterableType): The iterable to take elements from.
  • count (Int): The maximum number of elements to yield.

Returns:

_TakeIterator[IterableType.IteratorType[False, origin._mlir_origin, origin]]: An iterator that yields at most count elements.

def take[IterableType: IterableOwned, //](var iterable: IterableType, count: Int) -> _TakeIterator[IterableType.IteratorOwnedType]

Creates an iterator that yields the first count elements, consuming the iterable.

Parameters:

Args:

  • iterable (IterableType): The iterable to consume and take elements from.
  • count (Int): The maximum number of elements to yield.

Returns:

_TakeIterator[IterableType.IteratorOwnedType]: An iterator that yields at most count elements.