Find the first item of a dictionary


I made a program creating a dictionary, but the keys are random.

The issue is, I need to get the first element of this dictionary, but it won’t let me do dictionary[1] because it is not a list.

What should I do?

The dictionary part of a table has no notion of order. So nothing is first or second or last or whatever. You can instead do something like:

local t = {
    { key = "name", value = "Joe" },
    { key = "k", value = "v" }

print(t[1].key, t[1].value)

You can use the next function to retrieve the first item the dictionary iterator will iterate through:

local a = {}
a["test"] = 5
a["asdjh"] = "something"

local key, value = next(a, nil)
print(key, value)

Thank you! That’s what I wanted!

1 Like