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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:10:21+00:00 2026-06-06T08:10:21+00:00

Background I have two lists, the first is items which contains around 250 tuples,

  • 0

Background

I have two lists, the first is items which contains around 250 tuples, each tuple contains 3 elements

(path_to_a_file, size_in_bytes, modified_time)

The second list, result contains anywhere up to 250 elements, which is the result of a database query which looks up rows based on the paths that are in the items list. The number of elements in result depends if those files are in the database already.

each element in result is an row object returned from SQLAlchemy query with attributes for the row values, (path, mtime and hash are the ones I’m interested in here)

What I’m trying and do is filter out all the elements in items that are in results that have the same mtime (and keep track of the number, and total size filtered) and make a new list with items either with a different mtime or that dont exist in result. items with different mtimes need to be stored (path,size,mtime_from_result,hash_from_result) and items which weren’t in the database (path,size,mtime,None).

I hope I’m not making this too localised but I felt I needed to explain what I’m trying to accomplish to ask the question.

Problem

I want to try and make this loop as fast as possible but the most important part is making it work as expected.

Is it safe to remove items from the lists as I iterate over them? I noticed iterating forwards has a weird outcome but iterating backwards seems to be ok. Is there a better approach?

I’m removing items that I’ve matched up (i.path == j[0]) because I know the relationship is 1 to 1 and its not going to match again so by reducing the lists I can iterate over it faster in the next iteration, and more importantly I get left with all the unmatched items.

I can’t help feel there’s a much nicer solution that I’m overlooking, perhaps with list comprehension or generators perhaps.

send_items=[]
for i in result[::-1]:
    for j in items[::-1]:
        if i.path==j[0]:
            result.remove(i) #I think this remove is possibly pointless?
            items.remove(j)
            if i.mtime==j[2]:
                self.num_skipped+=1
                self.size_skipped+=j[1]
            else:
                send_items.append((j[0],j[1],i.mtime,i.hash))
            break
send_items.extend(((j[0],j[1],j[2],None) for j in items))
  • 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-06T08:10:23+00:00Added an answer on June 6, 2026 at 8:10 am

    I’d do this as:

    def get_send_items(items, results):
        send_items = []
        results_dict = {i.path:i for i in results}
        for p, s, m in items:
            result = results_dict.get(p)
            if result is None:
                send_items.append((p, s, m, None))
            elif result.mtime != m:
                send_items.append((p, s, result.mtime, result.hash))
        return send_items
    

    Here is my analysis of your solution (Assuming both result and items are of length N):

    • result[::-1] creates a copy of result so calling result.remove(i) doesn’t affect the iteration, nor would it have anyways. You only loop over result once, so removing elements is a bit pointless. It only creates extra work.
    • You could have called result[::] to create a copy of result.
    • Calling items.remove(j) actually reduces efficiency. remove() takes O(N) time. So calling it reduces the algorithm’s efficiency to O(N^3) from O(N^2).
    • By using O(N) extra memory (as in my solution) you can reduce the run time to O(N), if you use a dictionary or a set that has O(1) look ups.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, let me start with some background: I have a web service which accepts
I have a simple UITableView with two sections. Each section has a header which
I have two tabs: In first tab I have a fixed list of items.
Background: We have two directories of Crystal Reports for both the web side of
On this site: http://walkman.pk/aserdus2/tagok.php I have two background-images on the left and right side,
I have two images in the background of my html doc. One positioned top
I have two layers being drawn. A static background texture and a texture(png) with
If I have two css files: File 1: .colorme { background-color:Red; } File 2:
I need change background of all text that have two spaces from the start
How to automatically switch the background in layout android:background=@drawable/image> I have two pictures 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.