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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:55:22+00:00 2026-06-04T17:55:22+00:00

For a project I’m working on I am processing lots of deeply nested dict-and-list

  • 0

For a project I’m working on I am processing lots of deeply nested dict-and-list data structures. Often I find myself doing a lookup that I expect to return a list with a single member. Lookups may fail outright, or simply return zero results, so I could write code like this:

try:
    value_I_need = lookup_results[0]
except IndexError:
    # handle lookup failure, keep going

But lately I’ve been wondering if it would be more future-proof to write a loop, initially assuming it only happens zero or one times:

value_I_need = None
for value_I_need in lookup_results:
    break
if value_I_need is None:
    # handle lookup failure, keep going

(I say “future-proof” because I may want to re-write or generalize this code to handle lookups with more than one result in them.) Is there anything inherently unpythonic, wrong or slower about either of these two approaches?

  • 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-04T17:55:23+00:00Added an answer on June 4, 2026 at 5:55 pm

    The builtin function next() does exactly that:

    a = ()
    b = [1, 2, 3]
    print next(iter(a), "empty") # prints "empty", as a doesn't give any values.
    print next(iter(b), "empty") # prints 1, the 1st value of b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Project: Take Wikipedia's list of Roman consuls, put the data in a CSV so
Current project is an Mvc4 application, I had Ioc working and recently it just
The project i am working at right now requires some declarative way of defining
Project I'm working on uses jQuery. I have a series of Ajax calls being
Project A contains a WPF Window (Data Entry Form) with a stack panel of
Project file here if you want to download: http://files.me.com/knyck2/918odc So I am working on
Project Scenario Technology : Dotnetnuke (Approx. 100 - 150 screens) Data Architecture : LINQ
The project I am currently working on has, in the checkout, an option to
The project I'm working on has an Oracle backend, and we use SQL Navigator
The project I am working on will integrate with the customers Active Directory in

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.