0 votes

With a dictionary:

var dict = {
    "a": "hi",
    "b": "hello",
}

Using a for-each on dict seems to give the keys.

for i in dict:
    print(i)

prints:

a
b

Is there any difference between that and:

for i in dict.keys():

?

And, is there any way to iterate with a for-each over a dict in a way that gives you both keys and values? (Other than just fetching the value for the key inside the for-each)

asked May 2 in Engine by D_Steve595 (15 points)

1 Answer

+1 vote
Best answer

The main difference is that using dict.keys() has to allocate an array before iterating. Aside from that, they're identical.

And, is there any way to iterate with a for-each over a dict in a way that gives you both keys and values? (Other than just fetching the value for the key inside the for-each)

No, you'll have to do the latter.

answered May 2 by kidscancode (17,762 points)
selected May 2 by D_Steve595

Appreciate it!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.