Is it possible to get a key in a dictionary using its number (order)?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mymokol

In the documentation for the dictionary, there is stated that the keys in a dictionary preserve their order, even though it might not be so in printing.

I found myself needing to take every single key from a dictionary using a for loop, except the very first key. Is there a way to do that? If not, is there a way to just take every single key (once), no matter what the keys are and how many there are?

:bust_in_silhouette: Reply From: DDoop

You should be able to use the .keys() method to generate a list of keys, which you can then iterate over or slice.
The method in docs

Why of course, why didn’t I think of this when I read about it in the docs? Thank you very much!

mymokol | 2021-01-20 19:03

This is jus an exemple i did with a Spinbox Node Signal connected to a script and that plays one of this sounds when you pick a number (i hope it helps):

onready var soundFX = $AudioStreamPlayer
var beats = {
	"Q":"res://addons/Pack de Funk - Kylan/Batida #1 - 130 BPM.wav",
	"W":"res://addons/Pack de Funk - Kylan/Batida #2 - 130 BPM.wav",
	"E":"res://addons/Pack de Funk - Kylan/Batida #3 - 130 BPM.wav",
	"R":"res://addons/Pack de Funk - Kylan/Batida #4 - 130 BPM.wav",
	"T":"res://addons/Pack de Funk - Kylan/Batida #5 - 130 BPM.wav",
}

func _on_SpinBox_value_changed(value):
    	var keys = beats.keys()
    	var key = keys[value]
    	var stream = load(beats[key])
    	soundFX.set_stream(stream)
    	soundFX.play()

Diego Ferrari Bruno | 2021-06-23 19:24