Is new_arrary = [i for i in array if i >0] syntaxically correct in GDScript?

Godot Version



I’m pretty familiar with python and started to learn GDScript recently. I tried to write:

var array = [0, 1, 2, 3, 4]
var new_array = [i for i in array if i >= 3]

But it keeped reporting the same error:

‘Expected closing “]” after array elements.’

I can’t figure out the problem after checking the code over and over again. So I guess is this syntax not supported in GDScript? And I looked over the array part in documentation and didn’t find anything helpful.

List comprehension is not supported in GDScript.

You could use Array.reduce() to do that:

	var array = [0, 1, 2, 3, 4]
	var new_array = array.reduce(func(accum:Array, entry:int):
		if entry >= 3:
		return accum
	, [])


or just use a normal for loop.