What's the fastest way to iterate through a dictionarys values

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ulty
:warning: Old Version Published before Godot 3 was released.

I want to go through a large dictionary of rooms.
The key value pair is Vector2() and a pointer to the room for each element.

This would be my first approach but I don’t think that this would scale very well for a 1000+ room dictionary.

for i in rooms_dic.keys():

    var room = rooms_dic[i]

is there a way to get a array of the elements of a dictionary directly ?

Maybe you could have two representations of the container:
One to quickly iterate over all rooms (array), and another to quickly get a room from its position (dict).
Then you could maintain the two containers within a “room manager” or whatever class is in control of the rooms.

Zylann | 2016-04-10 19:54

Probably the way I will implement it, but I was hoping that there was an easier (lazier) way to do it.

ulty | 2016-04-10 20:05

I’m assuming what Zylann said actually might be an answer, this is the way how I’m doing this thing as well.

kubecz3k | 2016-04-11 15:24

:bust_in_silhouette: Reply From: duke_meister

So do you want to be ‘fast’ (as per your title) or ‘lazy’? Anyway, you can be slightly lazy by doing

for i in rooms_dic:
    var room =  rooms_dic[i]

(i.e. don’t need “.keys()”).

You can get an array of the values or keys by doing

var dict_key_array = Array(rooms_dic.keys())

or

var dict_val_array = Array(rooms_dic.values())

and then iterate the array. What more did you want?

var dict_val_array = Array(rooms_dic.values())

would be perfect but as far as I can tell there is no such function ( values() ) on base dictionary

ulty | 2016-04-11 04:45

ahh… so sorry… too much jumping between languages :confused:

duke_meister | 2016-04-11 04:47

:bust_in_silhouette: Reply From: ulty

The newer version of Godot now has a dictionary.values() function so this has been perfectly solved.