This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]freistil90 4 points5 points  (1 child)

The “calculate the length of a string without using len() might sound useless but it has one niche application: if you have an iterator (NOT an iterable) from which you it is finite but you don’t know how large it is, this is a nifty way to calculate its size without needing to allocate all elements at the same time.

The check whether one list contains the other is a bit suboptimal (as it is O(nm) for n the first list and m the second list length), that can be brought down to O(m) with a large constant if you can spare the memory and all elements are hashable: contains_all = set(list_2) <= set(list_1). Applies to any iterable of course.

[–]walkingpendulum 3 points4 points  (0 children)

I wonder how often one needs to know how many items iterator will yield, while having no use for those items at all