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

  • Home
  • SEARCH
  • 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 7732879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:50:32+00:00 2026-06-01T06:50:32+00:00

In C, it is considered bad practice to call strlen like this: for (

  • 0

In C, it is considered bad practice to call strlen like this:

for ( i = 0; strlen ( str ) != foo; i++ )
{
    // stuff
}

The reason, of course, is that it is inefficient since it “counts” the characters in a string multiple times.

However, in Python, I see code like this quite often:

for i in range ( 0, len ( list ) ):
    # stuff

Is this bad practice? Should I store the result of len() in a variable and use that?

  • 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-01T06:50:33+00:00Added an answer on June 1, 2026 at 6:50 am

    In Python, a for loop iterates through a list-like object, it doesn’t have a conditional statement that is checked each time. To illustrate, the following two loops are functionally equivalent; the while loop is a direct translation of for (i=0; i< n; i++) { ... }, while the for loop is the Pythonic way of doing it:

    i = 0
    while i < n:
       # do some actions using i
       i += 1
    
    for i in range(n):
       # do some actions using i
    

    In the for loop, range(n) is evaluated before the loop starts, and then its return value is used in the loop. In other words, range(n) is evaluated once (and only once) to get the list* of values that i should take.

    So in the specific example given, Python sees for i in range(0, len(list)), evaluates range(0, len(list)) (so len is called once) and stores the output (it doesn’t call range (or len) again).

    (* in Python 3.x range doesn’t actually return a list, but when using it in a for loop there is no difference in functionality)

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

Sidebar

Related Questions

Is it generally considered bad practice to provide Iterator implementations that are infinite; i.e.
Would this code be considered bad practice: <div id=sidebar> <% =DisplayMeetings(12) %> </div> This
It is generally considered bad practice to accept input that is derived from JavaScript
Are static classes considered bad practice? I read an article about this a couple
I know that having diamond inheritance is considered bad practice. However, I have 2
What is the technical reason why it is considered bad practice to use the
Would it be considered bad practice to have a viewmodel that has a property
Would this be considered bad practice? unless Link.exists?(:href => 'example.com/somepage') Domain.where(:domain => 'example.com').first.links.create(:href =>
Is it considered bad practice to have multiple try's in one method and structure
Is it considered bad practice to have multiple views for same URL in MVC,

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.