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

The Archive Base Latest Questions

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

I am trying to iterate through a list, and I need to perform specific

  • 0

I am trying to iterate through a list, and I need to perform specific operation when and only when the iteration reached the end of the list, see example below:

data = [1, 2, 3]

data_iter = data.__iter__()
try:
    while True:
        item = data_iter.next()
        try:
            do_stuff(item)
            break # we just need to do stuff with the first successful item
        except:
            handle_errors(item) # in case of no success, handle and skip to next item
except StopIteration:
    raise Exception("All items weren't successful")

I believe this code isn’t too Pythonic, so I am looking for a better way. I think the ideal code should look like this hypothetical piece below:

data = [1, 2, 3]

for item in data:
    try:
        do_stuff(item)
        break # we just need to do stuff with the first successful item
    except:
        handle_errors(item) # in case of no success, handle and skip to next item
finally:
    raise Exception("All items weren't successful")

Any thoughts are welcome.

  • 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-07T00:51:09+00:00Added an answer on June 7, 2026 at 12:51 am

    You can use else after a for loop, and the code within that else is only executed if you did not break out of the for loop:

    data = [1, 2, 3]
    
    for item in data:
        try:
            do_stuff(item)
            break # we just need to do stuff with the first successful item
        except Exception:
            handle_errors(item) # in case of no success, handle and skip to next item
    else:
        raise Exception("All items weren't successful")
    

    You can find this in the documentation for the for statement, relevant pieces shown below:

    for_stmt ::=  "for" target_list "in" expression_list ":" suite
                  ["else" ":" suite]
    

    A break statement executed in the first suite terminates the loop without executing the elseclause’s suite.

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

Sidebar

Related Questions

I am trying to iterate through a list of float widths that varies. [10.5,
I am trying to iterate through a list and take each part of the
So I'm trying to iterate through a list of files that are within a
I'm trying to iterate through a list of folders, but I can't seem to
Using SQLite3 with Python 2.5, I'm trying to iterate through a list and pull
Ok, What I need to do is iterate through a list of list and
I have a list within a list, and I am trying to iterate through
I am trying to iterate through a list of servers & passwords to change
I'm trying to iterate through a list of users folders in windows in c:\Users
I am trying to iterate through the SPListItem.Versions collection to find the latest approved

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.