python sets in gdscript

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

Is there any built-in object in gdscript that works kind of like Python’s sets? In particular, I want to keep adding things to it, but not store any duplicates of things I’ve already added.

:bust_in_silhouette: Reply From: kidscancode

I’m afraid not. GDScript only has Arrays (Lists) and Dictionaries for collection types. You’d have to build out your own set functionality using Array’s methods:

if !arr.has(item):
    arr.append(item)

See GDScript — Godot Engine (3.0) documentation in English

and

I know this is old, but it’s probably worth mentioning that it’s preferred to use Dictionary over List for a Set functionality, as the lookup is on the order of O(log(N)), as apposed to O(N) for Lists. It’s just the Dictionary value parameter can be anything and can be ignored.

Razzlegames | 2019-10-31 21:22

Isn’t the lookup for a Hashmap O(1)?

tsear | 2021-02-17 19:55