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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:41:10+00:00 2026-05-23T07:41:10+00:00

I have a data source which is best modeled with a dictionary (it is

  • 0

I have a data source which is best modeled with a dictionary (it is a collection of key=value pairs). For a specific visualization purpose, I need to provide a list-like data access interface (in addition to the regular dictionary interface), meaning that you should be able to do the following:

data["mykey"] # returns the associated value
data[12][0] # returns the 13th key in the dictionary
data[12][1] # returns the 13th value in the dictionary

I cannot find an appropriate facade implementation – if I store the indices as the dictionary key:

data[12] = ("mykey", "myval")

I can easily solve the last two cases, but I loose the ability to do the first. If I store data like

data["mykey"] = "myval"

I have to enumerate all keys and values in a temporary list before I can return elements.

Notice that all this implementations assume I am using an OrderedDict.

How would you provide both interfaces?

If you are curious, this is for creating a PyQt QAbstractTableModel where the underlying data container is a dictionary.

Thanks.

  • 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-23T07:41:11+00:00Added an answer on May 23, 2026 at 7:41 am

    I have to do the same thing to represent data in a ListCtrl that needs to be accessible by a key rather than by index at times (so that it does not have to be searched if I get an arbitrary value to locate). If you have a list of dictionaries, the best I found was to create another dictionary with references to the same items, but accessible by key. This becomes my data load in method:

      def SetData(self, cols, data):
        for idx, row in enumerate(data):
          item = dict((k, v.rstrip() if hasattr(v, 'rstrip') else v) for k, v in zip(cols, row))
    
          self.data[idx] = item
    
          self.byid[row[0]] = item
    

    So I have a list of dictionaries accessible in self.data, and then another dictionary in self.byid that keeps the same items, but by the id column (column 0 in my rows in this case). When I need to update, as long as I get an ID, I can call self.byid[id][field] = newval. Because everything in Python is a pointer (reference), changing the value of the dictionary stored in self.byid is reflected in the list of dictionaries stored in self.data. Works like a charm.

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

Sidebar

Related Questions

I have an XML Data Source which contains a list of key/value pairs. I'm
I have a custom NSTableView subclass which is bound to a data source (an
I have one table in my core data source which holds some articles with
I have an ETL application which extracts data from a source database (for which
I have written a Ruby application which parses lots of data from sources in
I have a slow custom data source in a SSIS Dataa Flow Task.I have
I have Visual Studio web test attached nicely to a data source, but I
I have several sources of tables with personal data, like this: SOURCE 1 ID,
I have a WPF application which saves its data to XML files in a
i have a input tag which is non editable, but some times i need

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.