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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:07:56+00:00 2026-06-02T04:07:56+00:00

What is more efficient in terms of memory and speed between d[(first,second)] and d[first][second]

  • 0

What is more efficient in terms of memory and speed between

d[(first,second)]

and

d[first][second],

where d is a dictionary of either tuples or dictionaries?

  • 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-02T04:07:58+00:00Added an answer on June 2, 2026 at 4:07 am

    Here is some very basic test data that indicates that for a very contrived example(storing ‘a’ a million times using numbers as keys) using 2 dictionaries is significantly faster.

    $ python -m timeit 'd = {i:{j:"a" for j in range(1000)} for i in range(1000)};a = [d[i][j] for j in range(1000) for i in range(1000)];'
    10 loops, best of 3: 316 msec per loop
    $ python -m timeit 'd = {(i, j):"a" for j in range(1000) for i in range(1000)};a = [d[i, j] for j in range(1000) for i in range(1000)];'
    10 loops, best of 3: 970 msec per loop
    

    Of course, these tests do not necessarily mean anything depending on what you are trying to do. Determine what you’ll be storing, and then test.

    A little more data:

    $ python -m timeit 'a = [(hash(i), hash(j)) for i in range(1000) for j in range(1000)]'
    10 loops, best of 3: 304 msec per loop
    $ python -m timeit 'a = [hash((i, j)) for i in range(1000) for j in range(1000)]'
    10 loops, best of 3: 172 msec per loop
    $ python -m timeit 'd = {i:{j:"a" for j in range(1000)} for i in range(1000)}'
    10 loops, best of 3: 101 msec per loop
    $ python -m timeit 'd = {(i, j):"a" for j in range(1000) for i in range(1000)}'
    10 loops, best of 3: 645 msec per loop
    

    Once again this is clearly not indicative of real world use, but it seems to me like the cost of building a dictionary with tuples like that is huge and that’s where the dictionary in a dictionary wins out. This surprises me, I was expecting completely different results. I’ll have to try a few more things when I have time.

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

Sidebar

Related Questions

Is there a more efficient (in terms of memory foot print) binding to datagrid
What is more efficient in Python in terms of memory usage and CPU consumption
What is more efficient in terms of memory and CPU usage — an array
Which of these is more efficient in terms of speed (number of lines of
Is there a more efficent way of doing this in terms of memory usage
I'm trying to figure out what is more efficient in terms of server load
My real Question is which of these is more efficient in terms of loading
I'm working on something that needs to be pretty efficient in terms of memory
Which is a more efficient way of accessing (key,value) pairs in terms of both
I'm curious as to which would be more cost efficient in terms of resources

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.