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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:42:00+00:00 2026-06-15T13:42:00+00:00

I am seeking some way to make this nested for loop more pythonic. Specifically,

  • 0

I am seeking some way to make this nested for loop more pythonic. Specifically, how can I iterate through unique combinations of three variables, and write to file if data is present in the dictionary?

foo,bar = {},{} #filling of dicts not shown
with open(someFile,'w') as aFile:
    for year in years:
        for state in states:
            for county in counties:
                try:foo[year,state,county],bar[state,county]
                except:continue
                aFile.write("content"+"\n")
  • 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-15T13:42:01+00:00Added an answer on June 15, 2026 at 1:42 pm

    You could just iterate over the keys of foo and then check if bar has a corresponding key:

    for year, state, county in foo:
        if (state, county) in bar:
            aFile.write(...)
    

    This way you avoid iterating over anything that won’t at least work for foo.

    The disadvantage to this is that you don’t know what order the keys will be iterated in. If you need them in sorted order you could do for year, state, county in sorted(foo).

    As @Blckknght pointed out in a comment, this method also will always write for every matching key. If you want to exclude some years/states/counties you could add that to the if statement (e.g., if (state, county) in bar and year > 1990 to exclude years before 1990, even if they are already in the dict).

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

Sidebar

Related Questions

I'm seeing if I can make some object oriented javascript and I have the
I am seeking some advice on the best way to retrieve and display my
Main Question: I'm seeking some way to give an object within a LinkedList a
First, a simple question. If this can't be done, I'll post a question seeking
I am newbie in Ruby.. seeking some help.. I have a code DB =
This is really a two part question: I'm seeing some users in the Grantee
I have this problem in my ASP.NET application where I'm seeing some of my
Is there a way to make a Radio Button enabled/disabled (not checked/unchecked) via CSS?
I'm struggling a bit with a table row colouring issue. I'm seeking some guidance:
This will be a question more for the application designers out there, but 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.