What's the difference between a table and a dictionary?

I don’t understand the difference. At first I thought that a table was something like:

local table = {
"Hi",
2,
nil
}

and a dictionary was:

local dictionary = {
Pet = "Dog",
Money = 0,
nothing = nil
}

but then I saw people call my definition of a dictionary a table. Can someone please elaborate on this for me?

9 Likes

Tables can be dictionaries, arrays, or mixed. Dictionaries are just a kind of table that only has unordered key value pairs.

3 Likes

I don’t understand. Can you please give me more information? Explain a dictionary and an array please, and how they can both be a table. :confused:

1 Like

Tables

Tables are both Arrays and Dictionaries, they can hold multiple datatypes and are really useful.
They have an “index” and a “value”, when you do something like: table[index] it’ll give you the value of that index.

Arrays

Arrays have ordered indexes starting from 1 to how ever many you give, in arrays you do not give the index - instead it gives the index itself.
An array will look like:

local Array = {3,2,1}

Loop through this and you’ll see how they’re ordered:

for index, value in pairs(Array) do
    print(string.format("%d | %d", index, value)) 
end
--[[
1 | 3
2 | 2
3 | 1
]]

Dictionaries

In Dictionaries you give the index, they’re not ordered.
A Dictionary will look like:

local Dictionary = {
    Index1 = 3;
    Index2 = 2;
    Index3 = 1;
}

Loop through this and you’ll see the specified indexes:

for index, value in pairs(Dictionary) do
    print(string.format("%s | %d", index, value))
end
--[[
Index1 | 3
Index2 | 2
Index3 | 1
]]

Dictionaries can also have spaces and non-numeric/non-alphabetic characters in their name though I don’t recommend it, here’s how:

local Dictionary = {
    ["Bro do"] = true;
    ["you even"] = true;
    ["lift?"] = true;
}
21 Likes

Thank you for the info! :smile: I get it now.

So basically, I guess what I thought was a table is actually an array.

3 Likes

Protip: If at all possible, don’t use mixed tables. In other words, use it as either a dictionary or as an array. While there are a few odd scenarios where using a mixed table works well, the vast majority of solutions are much easier to program around having a more rigid structure & will save you from weird bugs regarding iteration.

8 Likes

You seem to understand what a dictionarry actually is, but what’s confusing is when people call dictionarries tables. “Logically” speaking they are indeed called dictionarries, but it’s not really a problem to call them tables, because after all dictionarries are really just a special type of tables.

3 Likes

I guess a table would just be either/and/or a dictionary/array, right?

From what Crazyman32 said reminded me, the ipairs() iterator won’t include indexes that include non-numeric characters.

If you’re going to loop through a dictionary you’re going to have to use pairs() or next.

2 Likes

Well I’m not sure about what return said: the fact that arrays and dictionarries both fall on the table category. So basically an array is a table, and a dictionarry is a table. In my mind I always had the idea that table is just a synonym of array, and dictionarries are basically just tables but have a different way of organazing. Because after all, some other languages call arrays something different from tables, in python arrays are also called lists. Array is the universally accepted name, that might be different in other languages

Oh yeah, and this my post might interest you

1 Like

Thanks a lot! Also, tip for the future: “Dictionarries” is actually spelled “Dictionaries.”

Sorry for my very bad english xd

pairs doesn’t guarantee ordered iteration and it is meant for iteration where the key is arbitrary or unknown. Use ipairs to iterate through arrays, always. ipairs iterates at an i++ basis and is designed for working with arrays.

The output of your dictionary iteration is also incorrect. You put the values of the indexes in descending order rather than matching their index (I3 has a value of 1 but you put 3 in the output comment).

3 Likes