mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-25 01:26:30 -05:00
623 B
623 B
title | date | draft | tags | |
---|---|---|---|---|
Quick Python: Length of Iterables | 2020-03-25T18:28:09-04:00 | false |
|
I wanted to find the length of what I know is a finite iterable. Normally you would think of using the len
function but it does not work in this case. Al Hoo on StackOverflow shared a quick snippet to calculate this.
from functools import reduce
def ilen(iterable):
return reduce(lambda sum, element: sum + 1, iterable, 0)
This also turns out to be memory efficient since we are only loading in one object into memory from the iterable at a time.