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 3630740
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:18:30+00:00 2026-05-19T00:18:30+00:00

Is there a better way to write the following: row_counter = 0 for item

  • 0

Is there a better way to write the following:

   row_counter = 0
   for item in iterable_sequence:
      # do stuff with the item

      counter += 1

   if not row_counter:
      # handle the empty-sequence-case

Please keep in mind that I can’t use len(iterable_sequence) because 1) not all sequences have known lengths; 2) in some cases calling len() may trigger loading of the sequence’s items into memory (as the case would be with sql query results).

The reason I ask is that I’m simply curious if there is a way to make above more concise and idiomatic. What I’m looking for is along the lines of:

for item in sequence:
   #process item
*else*:
   #handle the empty sequence case

(assuming “else” here worked only on empty sequences, which I know it doesn’t)

  • 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-05-19T00:18:31+00:00Added an answer on May 19, 2026 at 12:18 am
    for item in iterable:
        break
    else:
        # handle the empty-sequence-case here
    

    Or

    item = next(iterator, sentinel)
    if item is sentinel:
       # handle the empty-sequence-case here   
    

    In each case one item is consumed if it is present.


    An example of empty_adapter()‘s implementation mentioned in the comments:

    def empty_adaptor(iterable, sentinel=object()):
        it = iter(iterable)
        item = next(it, sentinel)
        if item is sentinel:
           return None # empty
        else:
           def gen():
               yield item
               for i in it:
                   yield i
           return gen()
    

    You could use it as follows:

    it = empty_adaptor(some_iter)
    if it is not None: 
       for i in it:
           # handle items
    else:
       # handle empty case
    

    Introducing special case for an empty sequence for a general case seems wrong. There should be a better solution for a domain specific problem.

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

Sidebar

Related Questions

Is there a better way to write the following function? Having the '#' +
Is there a better/shorter way to write the whoAmI method in the following code?
Is there a better way to write the following conditional in javascript? if (
Is there a better way to write the following so that: a) fewer variables
Is there a better way to write the following? .parent .child_1, .parent .child_2, .parent
Is there a better way to write the following in my where clause? WHERE
Is there a better way to write the following? <cfloop list=#qry.Columnlist# index=FieldName> <cfset form.#FieldName#
Just wondering if there is a better way to write the following PL/SQL piece
Is there a better way to write the following? $(function(){ $('.start-here-notice').fadeIn(1000).animate({left:-155px}, slow) $('.start-here-notice').animate({left:-165px}, slow).delay(5000).fadeOut(1000);
Is there a better way to write this spec? This works but I don't

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.