Skip to content

list

A mutable sequence of elements, similar to Python's list. Supports negative indexing, slicing, and Python-style methods.

Properties

Name Type Description
is_read_only bool Gets a value indicating whether the list is read-only.

Methods

add(item: T)

Note

For collection initializers. Also a part of the ICollection interface.

copy() -> list[T]

Return a shallow copy of the list.

Returns: A new list with the same elements.

x = [1, 2, 3]
y = x.copy()    # [1, 2, 3]

sort(reverse: bool = False)

Sort the items of the list in place (the arguments can be used for sort customization, see Sorted() for their explanation).

Parameters:

  • reverse (bool) -- If True, sort in descending order.
x = [3, 1, 2]
x.sort()             # [1, 2, 3]
x.sort(reverse=True) # [3, 2, 1]

Note

Matches Python's stable sort for reference/nullable element types (the relative order of comparer-equal elements is preserved, and reverse preserves the original order among equal elements rather than reversing it). For value-type elements, where two comparer-equal elements are indistinguishable, the faster in-place sort is used.

sort(key: (T) -> TKey, reverse: bool = False)

Sort the items of the list in place (the arguments can be used for sort customization, see Sorted() for their explanation).

Note

Stable: the relative order of elements with equal keys is preserved, and reverse preserves that order rather than reversing it (matching Python). Unlike the keyless overload this is always stable, even for value-type elements, because the key projection makes equal-key ordering observable.

to_list() -> list[T]

Creates a shallow copy this list as a .NET list.

append(x: T)

Add an item to the end of the list. Similar to a[len(a):] = [x].

Parameters:

  • x (T) -- The item to add.
x = [1, 2, 3]
x.append(4)    # [1, 2, 3, 4]

extend(enumerable: IEnumerable[T])

Extend the list by appending all the items from the iterable. Similar to a[len(a):] = iterable.

Parameters:

  • enumerable (IEnumerable[T]) -- The iterable whose items are appended.
x = [1, 2]
x.extend([3, 4])    # [1, 2, 3, 4]

clear()

Remove all items from the list.

x = [1, 2, 3]
x.clear()    # []

insert(i: int, x: T)

Insert an item at a given position. The first argument is the index of the element before which to insert, so a.Insert(0, x) inserts at the front of the list, and a.Insert(Len(a), x) is equivalent to a.Append(x).

Parameters:

  • i (int) -- Index before which to insert.
  • x (T) -- The item to insert.
x = [1, 2, 3]
x.insert(0, 0)    # [0, 1, 2, 3]
x.insert(-1, 9)   # [0, 1, 2, 9, 3]

pop(i: int = -1) -> T

Remove the item at the given position in the list, and return it. If no index is specified, a.Pop() removes and returns the last item in the list. It raises an IndexError if the list is empty or the index is outside the list range.

Parameters:

  • i (int) -- Index of the item to remove (default: -1, the last item).

Returns: The removed item.

x = [1, 2, 3]
x.pop()     # 3, x is [1, 2]
x.pop(0)    # 1, x is [2]

Raises:

  • IndexError -- Thrown if the list is empty or the index is out of range.

remove(x: T)

Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.

Parameters:

  • x (T) -- The value to remove.
x = [1, 2, 3, 2]
x.remove(2)    # [1, 3, 2]

Raises:

  • ValueError -- Thrown if the value is not found.

reverse()

Reverse the elements of the list in place.

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

count(x: T) -> int

Return the number of times x appears in the list.

Parameters:

  • x (T) -- The value to count.

Returns: The number of occurrences.

x = [1, 2, 2, 3]
x.count(2)    # 2
x.count(5)    # 0

index(x: T, start: int = 0, end: int = int.MaxValue) -> int

Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.

Parameters:

  • x (T) -- The value to search for.
  • start (int) -- Start of the slice to search (default: 0).
  • end (int) -- End of the slice to search (default: -1, end of list).

Returns: The zero-based index of the first matching item.

x = [1, 2, 3, 2]
x.index(2)       # 1
x.index(2, 2)    # 3

Note

The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.

Raises:

  • ValueError -- Thrown if the value is not found.

contains(x: T) -> bool

Returns whether the item is in the list.

Parameters:

  • x (T) -- The value to check for.

Returns: True if the item is found; otherwise False.

x = [1, 2, 3]
2 in x    # True
5 in x    # False

__str__() -> str

repr() uses the same method. Returns a string representation of this list.

get_slice(slice: slice) -> list[T]

Returns a slice of the list.

Raises:

  • ValueError -- Thrown if slice step is zero.

set_slice(slice: slice, other: IEnumerable[T])

Sets a slice of the list from an enumerable.

set_slice(slice: slice, other: list[T])

Sets a slice of the list from another list.

Raises:

  • TypeError -- Thrown if other is null.
  • ValueError -- Thrown if slice step is zero or assignment size mismatches extended slice.

delete_slice(slice: slice)

Deletes a slice of the list.

Raises:

  • ValueError -- Thrown if slice step is zero.

copy_to(array: array[T], array_index: int)

Copies the elements of the list to an array.