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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:50:54+00:00 2026-05-31T23:50:54+00:00

In short: what’s the fasted way to check if a huge list in python

  • 0

In short: what’s the fasted way to check if a huge list in python has changed? hashlib needs a buffer, and building a string representation of that list is unfeasible.

In long: I’ve got a HUGE list of dictionaries representing data. I run a number of analyses on this data, but there are a few meta-data aspects that are required by all of the analyses, ie. the the set of subjects (each dict in the list has a subject key, and at times I just need a list of all subject who have data present in the data set.). So I’d like to implement the following:

class Data:
    def __init__(self, ...):
        self.data = [{...}, {...}, ...] # long ass list of dicts
        self.subjects = set()
        self.hash = 0

    def get_subjects(self):
        # recalculate set of subjects only if necessary
        if self.has_changed():
            set(datum['subject'] for datum in self.data)

        return self.subjects

    def has_changed(self):
        # calculate hash of self.data
        hash = self.data.get_hash() # HOW TO DO THIS?
        changed = self.hash == hash
        self.hash = hash # reset last remembered hash
        return changed

The question is how to implement the has_changed method, or more specifically, get_hash (each object already has a __hash__ method, but by default it just returns the object’s id, which doesn’t change when we e.g. append an element to a list).

  • 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-31T23:50:55+00:00Added an answer on May 31, 2026 at 11:50 pm

    A more sophisticated approach there would be to work with proxy data elements instead of native lists and dictionaries, which could flag any change to their attributes. To make it more flexible, you could even code a callback to be used in case of any changes.

    So, assuming you only have to deal with lists and dictionaries on your data structure – we can work with classes inheriting from dict and list with a callback when any data changing method on the object is accessed The full list of methods is in http://docs.python.org/reference/datamodel.html

    # -*- coding: utf-8 -*-
    # String for doctests and  example:
    """
                >>> a = NotifierList()
                >>> flag.has_changed
                False
                >>> a.append(NotifierDict())
                >>> flag.has_changed
                True
                >>> flag.clear()
                >>> flag.has_changed
                False
                >>> a[0]["status"]="new"
                >>> flag.has_changed
                True
                >>> 
    
    """
    
    
    changer_methods = set("__setitem__ __setslice__ __delitem__ update append extend add insert pop popitem remove setdefault __iadd__".split())
    
    
    def callback_getter(obj):
        def callback(name):
            obj.has_changed = True
        return callback
    
    def proxy_decorator(func, callback):
        def wrapper(*args, **kw):
            callback(func.__name__)
            return func(*args, **kw)
        wrapper.__name__ = func.__name__
        return wrapper
    
    def proxy_class_factory(cls, obj):
        new_dct = cls.__dict__.copy()
        for key, value in new_dct.items():
            if key in changer_methods:
                new_dct[key] = proxy_decorator(value, callback_getter(obj))
        return type("proxy_"+ cls.__name__, (cls,), new_dct)
    
    
    class Flag(object):
        def __init__(self):
            self.clear()
        def clear(self):
            self.has_changed = False
    
    flag = Flag()
    
    NotifierList = proxy_class_factory(list, flag)
    NotifierDict = proxy_class_factory(dict, flag)
    

    2017 update

    One does live and learn: native lists can be changed by native methods by calls that bypass the magic methods. The fool proof system is the same approach, but inheriting from collections.abc.MutableSequence instead, nd keeping a native list as an internal attribute of your proxy object.

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

Sidebar

Related Questions

Short version: I'm trying to determine the best way to track what the user
Short of recursing and comparing dates of files/directories, is there a better way of
Short Generating table with barcodes of items. Each item has exact quantity in database
Short question: is there a way to make a jsonp request to a server,
Short question: is there a way to access the datatypes available in .net application
Short of cutting and pasting, is there a way to sort the methods in
Short of copying the entire .netbeans directory is there any way to transfer custom
Short of rolling your own. There has to be something out there. FlexLM/FlexNet is
Short version of Problem Autocomplete works when the input string matches the result string,
Short Question : Since DNS is anycast, is there any way for a DNS

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.