Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8235587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:51:38+00:00 2026-06-07T18:51:38+00:00

Many of Python’s built-in functions ( any() , all() , sum() to name some)

  • 0

Many of Python’s built-in functions (any(), all(), sum() to name some) take iterables but why does len() not?

One could always use sum(1 for i in iterable) as an equivalent, but why is it len() does not take iterables in the first place?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:51:39+00:00Added an answer on June 7, 2026 at 6:51 pm

    Many iterables are defined by generator expressions which don’t have a well defined len. Take the following which iterates forever:

    def sequence(i=0):
        while True:
            i+=1
            yield i
    

    Basically, to have a well defined length, you need to know the entire object up front. Contrast that to a function like sum. You don’t need to know the entire object at once to sum it — Just take one element at a time and add it to what you’ve already summed.

    Be careful with idioms like sum(1 for i in iterable), often it will just exhaust iterable so you can’t use it anymore. Or, it could be slow to get the i’th element if there is a lot of computation involved. It might be worth asking yourself why you need to know the length a-priori. This might give you some insight into what type of data-structure to use (frequently list and tuple work just fine) — or you may be able to perform your operation without needing calling len.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All the Python built-ins are subclasses of object and I come across many user-defined
Not many are aware of this feature, but Python's functions (and methods) can have
I normally write code with tabs but many python libraries use spaces. Is there
There are so many html and xml libraries built into python , that it's
what's the easiest way to take the intersection of N-many lists in python? if
In python I have many functions likes the ones below. I would like to
There are many escape functions in the Python module mysqldb whose documentation I don't
I have a system with many versions of python. Some are 32b some 64b,
There are many threads on find replace text for python but I think my
I have a python class with many methods(): Method1() Method2() ........... ........... MethodN() All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.