list

Store items in order, including repeated values. Read positions starting at 0, loop through items, or add, replace and remove items in the same list.

Syntax

list(iterable=(), /)

Returns

Example

numbers = list()
numbers.append(2)
numbers.extend([0, 3])
first = numbers[0]        # returns 2
count = len(numbers)      # returns 3

for value in numbers:
    wait(50)              # returns None

Methods

22 methods

list.constructor

Create an empty list or a new list from a source you can loop through, such as a string, list or generator. Items keep their order; nested collections are shared with the source.

Syntax

list()
list(iterable, /)

Parameters

NameDescription
iterableIterable
Source values to copy into the new list.
Depends on overload · Positional only
Default: ()

Returns

list — A new mutable list.

Example

numbers = list()
numbers.append(2)
numbers.append(3)

copied = list(numbers)

list.literal

Create a list by writing its items between square brackets.

Syntax

[1, 2, 3]

Parameters

NameDescription
itemAny?
Each item written inside the brackets.
Required · Positional or named
No default

Returns

list — A new list with items in the written order.

Example

numbers = [2, 0, 3]
first = numbers[0]       # returns 2

list.comprehension

Build a list from values produced by expr while looping through a source. Use if to keep only matching items; multiple for/if clauses are allowed. Loop variables stay inside this expression.

Syntax

[expr for target in iterable if condition]

Parameters

NameDescription
exprAny?
Value to append for each accepted item.
Required · Positional or named
No default
targetName | destructuring target
Variable or group of variables receiving the current item. A target such as *rest is not supported.
Required · Positional or named
No default
iterableIterable
Source you can loop through, such as a string, collection or generator; a dictionary yields its keys.
Required · Positional or named
No default
conditionBoolean
Optional filter checked before appending.
Optional · Positional only
Default: True

Returns

list — A new mutable list with matching items in iteration order.

Example

values = [1, 2, 3, 4]
evens = [x for x in values if x % 2 == 0]

list.range_source

Create a new integer list for loops, counters, and numeric ranges.

Syntax

range(stop, /) | range(start, stop, step=1, /)

Parameters

NameDescription
startNumber
Start value (inclusive).
Depends on overload · Positional only
Default: 0
stopNumber
End value (exclusive).
Required · Positional only
No default
stepNumber
Amount added for each next item. A negative value counts down; 0 raises an error. If the step points away from stop, the result is empty.
Depends on overload · Positional only
Default: 1

Returns

list[Int] — A new integer list.

Example

numbers = range(5)         # returns [0, 1, 2, 3, 4]
evens = range(0, 10, 2)   # returns [0, 2, 4, 6, 8]

list.length

Count how many items are in the list.

Syntax

len(list, /)

Parameters

NameDescription
listlist
The list to count.
Required · Positional only
No default

Returns

Int — Number of items in the list.

Example

numbers = range(3, 8)     # returns [3, 4, 5, 6, 7]
count = len(numbers)      # returns 5

list.index_access

Read one item by position: 0 is first, -1 is last, and other negative indexes count from the end.

Syntax

list[index]

Parameters

NameDescription
listlist
The source list.
Required · Positional or named
No default
indexInt
Item position: 0 is first, -1 is last.
Required · Positional only
No default

Returns

Any? — The item at that position. Raises IndexError when the index is outside the list.

Example

numbers = range(10, 15)   # returns [10, 11, 12, 13, 14]
first = numbers[0]        # returns 10
third = numbers[2]        # returns 12
last = numbers[-1]        # returns 14

list.slice

Return a new list from start up to, but not including, end. Negative indexes count from the end. Omitted bounds select the whole list in the step direction; nested collections remain shared.

Syntax

list[start:end[:step]]

Parameters

NameDescription
startInt
First item index to include.
Optional · Positional only
Default: start of list
endInt
Stop before this item index.
Optional · Positional only
Default: end of list
stepInt
Distance between items. Cannot be 0; negative values read backward.
Optional · Positional only
Default: 1

Returns

list — A new mutable list slice, clamped to the available range.

Example

numbers = [0, 1, 2, 3, 4]
middle = numbers[1:4]     # returns [1, 2, 3]
tail = numbers[-2:]       # returns [3, 4]
reverse = numbers[::-1]   # returns [4, 3, 2, 1, 0]

list.index_assignment

Replace one existing item by index. Negative indexes count from the end. The index must already be inside the list.

Syntax

list[index] = value

Parameters

NameDescription
listlist
The mutable list to update.
Required · Positional or named
No default
indexInt
Item index that already exists. Use -1 for the last item.
Required · Positional only
No default
valueAny?
New value to store at that position.
Required · Positional only
No default

Returns

None — No direct return value. The list is updated in place.

Example

numbers = [12, 12, 21, 3, 2, 1]
numbers[4] = 3            # numbers becomes [12, 12, 21, 3, 3, 1]

list.destructuring_assignment

Assign list items to variables from left to right. The number of variables must match the number of items or an error is raised. Groups of variables can be nested; *rest is not supported.

Syntax

a, b = list

Parameters

NameDescription
a, bTargets
Variables receiving the items; their count must match the list length.
Required · Positional or named
No default
listlist
Source list to unpack.
Required · Positional only
No default

Returns

None — No direct return value. Targets receive list items left to right.

Example

point = [500, 900]
x, y = point

list.append

Add one new item to the end of the list.

Syntax

list.append(value, /)

Parameters

NameDescription
valueAny?
The value to add.
Required · Positional only
No default

Returns

None — No direct return value. The list is updated in place.

Example

numbers = [2, 0]
numbers.append(3)
last = numbers[-1]        # returns 3

list.extend

Add each item from a source you can loop through to the end of this list, preserving source order.

Syntax

list.extend(iterable, /)

Parameters

NameDescription
iterableIterable
Source values whose items will be appended.
Required · Positional only
No default

Returns

None — No direct return value. The list grows in place.

Example

numbers = [1]
numbers.extend([2, 3])
count = len(numbers)      # returns 3

list.insert

Insert an item before the given position. Positions start at 0; negative indexes count from the end. An index before the list inserts at the beginning; one past the list inserts at the end.

Syntax

list.insert(index, value, /)

Parameters

NameDescription
indexInt
Position before which the value is inserted.
Required · Positional only
No default
valueAny?
The value to insert.
Required · Positional only
No default

Returns

None — No direct return value. The list is updated in place.

Example

numbers = [1, 3]
numbers.insert(1, 2)      # numbers becomes [1, 2, 3]

list.remove

Remove the first item whose value matches the given value.

Syntax

list.remove(value, /)

Parameters

NameDescription
valueAny?
Value to find and remove from the list.
Required · Positional only
No default

Returns

None — No direct return value. Raises ValueError when the value is not found.

Example

numbers = [1, 2, 2, 3]
numbers.remove(2)         # numbers becomes [1, 2, 3]

list.pop

Remove one item from the list and return it. Omitting the index removes the last item.

Syntax

list.pop(index=-1, /)

Parameters

NameDescription
indexInt
Optional index to remove. Negative indexes count from the end.
Optional · Positional only
Default: -1

Returns

Any? — The removed item. Raises IndexError when the list is empty or the index is outside the list.

Example

numbers = [2, 0, 3]
removed = numbers.pop()   # returns 3
first = numbers.pop(0)    # returns 2

list.clear

Remove every item from the list.

Syntax

list.clear()

Returns

None — No direct return value. The list becomes empty.

Example

numbers = [2, 0, 3]
numbers.clear()
text(str(len(numbers)))   # returns 0

list.index

Return the first index whose item equals value within the optional start and stop bounds.

Syntax

list.index(value, start=0, stop=len(list), /)

Parameters

NameDescription
valueAny?
Value to search for.
Required · Positional only
No default
startInt
First position to search, included. Positions start at 0; negative values count from the end.
Optional · Positional only
Default: 0
stopInt
Stop before this position. Negative values count from the end.
Optional · Positional only
Default: len(list)

Returns

Int — The matching index. Raises ValueError when the value is not found.

Example

numbers = [1, 2, 1, 3]
first_one = numbers.index(1)       # returns 0
later_one = numbers.index(1, 1)    # returns 2

list.count

Count how many items equal the given value.

Syntax

list.count(value, /)

Parameters

NameDescription
valueAny?
Value to count.
Required · Positional only
No default

Returns

Int — Number of matching items.

Example

numbers = [1, 2, 1, 3]
ones = numbers.count(1)   # returns 2

list.sort

Reorder this list from smallest to largest. Equal comparison values keep their original order. Values must be comparable: numbers and booleans, strings, or lists/tuples of the same kind with comparable items; mixing incompatible kinds raises an error.

Syntax

list.sort(*, key=None, reverse=False)

Parameters

NameDescription
keyCallable?
Optional function called once per item to produce the value used for sorting. None compares the items themselves.
Optional · Keyword only
Default: None
reverseBoolean
When true, sort from largest to smallest.
Optional · Keyword only
Default: False

Returns

None — No direct return value. The list is reordered in place.

Example

numbers = [3, 1, 2]
numbers.sort()
text(str(numbers))        # returns [1, 2, 3]

list.reverse

Reverse the list in place.

Syntax

list.reverse()

Returns

None — No direct return value. The list order is reversed in place.

Example

numbers = [1, 2, 3]
numbers.reverse()         # numbers becomes [3, 2, 1]

list.copy

Return a new list with the same items. Adding, removing or replacing items in the copy does not change the original; nested collections are still shared.

Syntax

list.copy()

Returns

list — A new list; nested collections are not copied.

Example

numbers = [1, 2]
copied = numbers.copy()
copied.append(3)
count = len(numbers)      # returns 2

list.for_loop

Loop through items in order from left to right.

Syntax

for item in list:

Parameters

NameDescription
itemAny
Current item inside the loop.
Required · Positional or named
No default
listlist
The list to iterate.
Required · Positional or named
No default

Returns

None — No direct return value. The loop runs its body once per item.

Example

steps = range(1, 4)      # returns [1, 2, 3]
for step in steps:
    wait(step * 100)      # returns None

list.truthy_check

Check whether the list has at least one item. An empty list is treated as false.

Syntax

if list | if not list

Parameters

NameDescription
listlist
The list used in the condition.
Required · Positional or named
No default

Returns

Boolean condition — true when the list is not empty, false when it is empty.

Example

numbers = range(0)        # returns []
if not numbers:
    text("No items")      # returns None