Menu
CoddyTech

Array and List APIs

Lesson 12 of 48 in Coddy's Python for GDScript Developers course.

Godot Array and Python list are ordered, growable containers with zero-based and negative indexing. Their APIs differ: GDScript size() becomes len, append remains a mutation, and Python slicing uses colon syntax. A Python slice creates a new outer list and excludes the stop index. Packed Godot arrays and typed Arrays are engine types, not Python list type annotations.

GDScript

extends SceneTree
func _init():
    var items = [4, 8]
    items.append(12)
    print(items[-1])
    quit()

Python

items = [4, 8]
print(items.append(12))
print(items[-1])

Porting trap

Python append returns None, so do not assign its result as if it were the new list. Python has no built-in PackedInt64Array equivalent required by these tasks.

Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.

challenge icon

Challenge

Beginner

Read four integers on four lines. Create a list from the first three, append the fourth, then remove the first item with pop(0). Print the removed integer, the resulting last item, and the first two items of the resulting list on separate lines. Print the last line in Python list format, such as [2, 3]. Values range from -20 to 20.

Print only the requested output, with a newline after each line. Keep the supplied input scaffold. No loops or functions are needed.

Your code goes in main.py. The locked driver.py starts your program and cannot be edited.

Try it yourself

# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module

run_module("main", run_name="__main__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for GDScript Developers

Practice on your own: Online Python compiler