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

The Archive Base Latest Questions

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

I think I want to make a 2D dictionary with multiple keys per value.

  • 0

I think I want to make a 2D dictionary with multiple keys per value.

I know how to make a 2D dictionary using defaultdict:

from collections import defaultdict
2d_dict = defaultdict(dict)

2d_dict['canned_food']['spam'] = 'delicious'

And I know that using regular dictionaries you can make with multiple keys like:

dictionary={('food','canned_food'):spam}

But I want to do something like lookup by tuple-of-keys:

2d_dict[('canned_food','food')]['spam'] = 'delicious'

In the first dimension of dictionary I need ~25 keys per value. Is there a way to do this with defaultdict?

Even if there is a way to do it with dicts is this a reasonable way to make a simple multidimensional lookup table?

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

    Apart from 2d_dict being an invalid variable name (it starts with a digit), your existing solution already works:

    >>> from collections import defaultdict
    >>> d2_dict = defaultdict(dict)
    >>> d2_dict[('canned_food', 'food')]['spam'] = 'delicious'
    >>> d2_dict
    defaultdict(<type 'dict'>, {('canned_food', 'food'): {'spam': 'delicious'}})
    

    In fact, you don’t even need the parentheses – Python will still recognise your key as a tuple:

    >>> d2_dict['fresh_food', 'food']['eggs'] = 'delicious'
    >>> d2_dict
    defaultdict(<type 'dict'>, {('canned_food', 'food'): {'spam': 'delicious'},
    ('fresh_food', 'food'): {'eggs': 'delicious'}})
    

    … and, yes, it’s a perfectly reasonable way to build a 2D+1D lookup table.

    If you want to build a 3D lookup table using nested dicts instead of tuple keys, this works:

    >>> d3_dict = defaultdict(lambda: defaultdict(dict))
    >>> d3_dict['dried_food']['food']['jerky'] = 'chewy'
    >>> d3_dict
    defaultdict(<function <lambda> at 0x7f20af38a2a8>, 
    {'dried_food': defaultdict(<type 'dict'>, {'food': {'jerky': 'chewy'}})})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a 2d dictionary with multiple keys per value. I do
I want to make something that makes much sense, but I think its not
I think it's not possible, but I want to make sure.
I want to have a dictionary that assigns a value to a set of
I want to make my application compatible with as2, so I think what I
when using a makefile, I want the following to be possible: make clean make
Just want to make sure one thing. In a windows machine (either a desktop
I want to make a website in cakephp framework.but the important thing is I
I think I want to use pythons built in calendar module to create an
Simple problem (I think): I want to be able to invoke a click method

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.