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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:51:14+00:00 2026-05-25T02:51:14+00:00

I have a really ugly loop in my code which is really slowing down

  • 0

I have a really ugly loop in my code which is really slowing down my program. The loop basically performs a dictionary comparison where, if a specific key in dict_A is the same as in dict_B, then for all matches a sort is performed which is written to a file.

for k, v in A_dict.items():
    for i, value in B_dict.items():
        if k == value[0]:
            sorted_B = [list(value) for key, value in groupby(sorted(B_dict.values()), key=itemgetter(1,2))]
            outfile.write('{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n'.format (i, k, v, value[1], value[2], value[3])

Unfortunately, the dictionaries both contain over a million items. Other than putting this data into a database then sorting, does anyone have any suggestions on how to speed up this loop? Thanks for the help.

  • 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-25T02:51:14+00:00Added an answer on May 25, 2026 at 2:51 am

    Your example code may be inaccurate, but as written,

    sorted_B = [list(value) for key, value in 
                    groupby(sorted(B_dict.values()), key=itemgetter(2,3))]
    

    will be the same every time… why is it in a loop at all?

    Also

    for k, v in A_dict.items():
        for i, value in B_dict.items():
            if k == value[0]:
                outfile.write('{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n'.format(
                    i, k, v, value[1], value[2], value[3])
    

    Looks like it could just be written as

    for i, value in B_dict.items():
        k = value[0]
        if k in A_dict:
            outfile.write('{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n'.format(
                i, k, A_dict[k], value[1], value[2], value[3])
    

    Which should be faster — it’s linear time rather than quadratic time.

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

Sidebar

Related Questions

I have a database table full of some really ugly and messy data. In
I've got a program where a lot of classes have really complicated configuration requirements.
We’ve found that the unit tests we’ve written for our C#/C++ code have really
What should i use to code Classic ASP under Linux. I have really tried
I have some really complicated legacy code I've been working on that crashes when
I have a really ugly legacy database system that I need to integrate with.
I have code in the following form in which the user may specify a
I have a critical loop in my code with this shape : int myloop(int
I've hacked together something that works flawlessly but it's really ugly code and I
I wrote this code. It's really ugly. I would like to refactor it. I

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.