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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:54:30+00:00 2026-06-03T01:54:30+00:00

Is there a functionality in Lua similar to collections.defaultdict available in Python, which automatically

  • 0

Is there a functionality in Lua similar to collections.defaultdict available in Python, which automatically handles default values for non-existent associative array keys?

I want the code below to set nil to v instead of an error. So basically a way to a[2] (non-existent key) be a table by default:

a = {}
v = a[2][3] 

>>> PANIC: unprotected error in call to Lua API (main.lua:603: attempt to index field '?' (a nil value))

In Python it can be done like this:

>>> import collections
>>> a = collections.defaultdict(dict)
>>> print a[2]
{}
  • 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-03T01:54:32+00:00Added an answer on June 3, 2026 at 1:54 am

    Is there a Lua standard function to do it? No. But you can do it easily enough with metatables. You can even write a function to create such tables:

    function CreateTableWithDefaultElement(default)
      local tbl = {}
      local mtbl = {}
      mtbl.__index = function(tbl, key)
        local val = rawget(tbl, key)
        return val or default
      end
      setmetatable(tbl, mtbl)
      return tbl
    end
    

    Note that each element will get the same default value. So if you make the default value a table, each “empty” element in the returned table will effectively reference the same table. If that’s not what you want, you’ll have to modify the function.

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

Sidebar

Related Questions

Is there any default functionality for arranging JInternalFrames in Java Swing? I would like
Is there an available API or functionality in cocoa where i can communicate with
Is there anything equivalent or close in terms of functionality to Python's virtualenv ,
Is there any utility or plugin which provides cscope like functionality for C++. I
Using Sql Server 2008, is there any functionality similar to #region in Visual Studio?
Is there any functionality in Eclipse which would generate empty function implementations by their
Say there is some functionality needed for an application under development which could be
Is there any functionality in Java for pausing the program like system(PAUSE); does in
There is a functionality in my module, where the user can scan the number
In russian social network there is a functionality to play music online. Nothing special

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.