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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:34:07+00:00 2026-06-14T07:34:07+00:00

How is possible in Python to create a dictionary where the keys are pairs

  • 0

How is possible in Python to create a dictionary where the keys are pairs of integers?

For example, if I do this:

mydict=dict()
mydict[ [1,2] ] = 'xxx'

I get the error TypeError: unhashable type: 'list'

So I came up with two different solutions: strings or tuples as keys.

A first solution seems to convert the pair of integers in their string representation:

mydict=dict()
mydict[ str(1)+" "+str(2) ] = 'xxx'

while the second solution involves tuples:

mydict=dict()
mydict[ tuple([1,2]) ] = 'xxx'

From some experiments I’ve found that the tuple solution is slower than the string one.
Is there a more efficient and fast way to use simply two integers as keys?

  • 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-14T07:34:09+00:00Added an answer on June 14, 2026 at 7:34 am

    You should probably use a tuple, which can be hashed:

    mydict = {}
    mydict[(1, 2)] = 'xxx'
    # or more concisely (@JamesHenstridge):
    mydict[1,2] = 'xxx'
    

    If that is actually too slow (don’t optimise unnecessarily), then given a maximum value for the one integer, construct an index:

    def index(a, b, maxB):
        return a*maxB + b
    
    mydict[index(1, 2, max)] = 'xxx'
    

    But be aware that a function call could easily slow it down further, so you can inline the function at the cost of readability and making it easier to introduce bugs if copy-pasted elsewhere:

    mydict[1*max + 2] = 'xxx'
    

    Incidentally, there is an SO question on read speeds of dictionaries with tuple keys:

    Python tuples as keys slow?

    Doing a tiny bit of profiling showed the inline index to be marginally (<5%) faster than the tuple, and both about twice as fast as the index. If this was done in PyPy, I would expect the index version (inline or not) to be faster.

    On a subsidiary note; if you are worrying about the insertion speed into a dict, you may be using the wrong data structure, or perhaps doing more work than necessary. As an example, parsing a CSV file into fields in each line and storing the values in a dict this way data[line,field] may be unnecessary if you can make the line parsing lazy and only parse the lines that you actually pull data out of. I.e. don’t do data = parseAll(somecsv); print data[7,'date'] when you can do dataLines = somecsv.readlines(); print getField(dataLines[7], 'date').

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

Sidebar

Related Questions

Is it possible to create an object from a dictionary in python in such
I'm putting around 4 million different keys into a Python dictionary. Creating this dictionary
Is it possible for the python library NLTK to suggest/create synonyms for groups of
is it possible to create a custom NaN value in Python? I know a
Is it possible to create an environment to safely run arbitrary Python scripts under
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm trying to create
Possible Duplicate: Python: Get object by id Typically when you see the string representation
Usually when I code in Python, I have to create a dictionary, and I
is it possible to use python to create flash like browser games? (Actually I
Is it possible in Python to create two immutable objects with the same value?

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.