2.2. ListView class

class immutable_views.ListView(a_list)[source]

An immutable list view.

Derived from Sequence.

This is an immutable view on an original (mutable) Sequence object, e.g. list or tuple.

The view class supports the complete Python list behavior, except for any operations that would modify the list. More precisely, the view class supports all methods of Sequence (the methods are listed in the table at the top of the linked page).

The view is “live”: Since the view class delegates all operations to the original list, any modification of the original list object will be visible in the view object.

Note that only the view object is immutable, not its items. So if the values in the original list are mutable objects, they can be modified through the view.

Note that in Python, augmented assignment (e.g. += is not guaranteed to modify the left hand object in place, but can result in a new object. For details, see object.__iadd__(). The += operator on a left hand object that is a ListView object results in a new ListView object on a new list object.

Parameters

a_list (Sequence) – The original list. If this object is a ListView, its original list is used.

Methods:

__add__(other)

self + other: Return a new view on the concatenation of the list and the other list.

__contains__(value)

value in self: Return a boolean indicating whether the list contains a value.

__eq__(other)

self == other: Return a boolean indicating whether the list is equal to the other list.

__ge__(other)

self < other: Return a boolean indicating whether the list is greater than or equal to the other list.

__getitem__(index)

self[index]:

__gt__(other)

self > other: Return a boolean indicating whether the list is greater than the other list.

__iter__()

Return an iterator through the list items.

__le__(other)

self < other: Return a boolean indicating whether the list is less than or equal to the other list.

__len__()

len(self): Return the number of items in the list.

__lt__(other)

self < other: Return a boolean indicating whether the list is less than the other list.

__mul__(number)

self * number: Return a new view on the multiplication of the list with a number.

__ne__(other)

self != other: Return a boolean indicating whether the list is not equal to the other list.

__repr__()

repr(self): Return a string representation of the view suitable for debugging.

__reversed__()

reversed(self) ...: Return an iterator through the list in reversed iteration order.

__rmul__(number)

number * self: Return a new view on the multiplication of the list with a number.

copy()

Return a new view on a shallow copy of the list.

count(value)

Return the number of times the specified value occurs in the list.

index(value[, start, stop])

Return the index of the first item in the list with the specified value.

__add__(other)[source]

self + other: Return a new view on the concatenation of the list and the other list.

The returned ListView object is a view on a new list object of the type of the left hand operand that contains the items that are in the original list of the left hand operand, concatenated with the items in the other list (or in case of a ListView, its original list).

The other object must be an iterable or ListView.

The list and the other list are not changed.

Raises

TypeError – The other object is not an iterable.

__contains__(value)[source]

value in self: Return a boolean indicating whether the list contains a value.

The return value indicates whether the original list contains an item that is equal to the value.

__eq__(other)[source]

self == other: Return a boolean indicating whether the list is equal to the other list.

The return value indicates whether the items in the original list are equal to the items in the other list (or in case of a ListView, its original list).

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__ge__(other)[source]

self < other: Return a boolean indicating whether the list is greater than or equal to the other list.

The return value indicates whether the original list is greater than or equal to the other list (or in case of a ListView, its original list), based on the lexicographical ordering Python defines for sequence types (see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types)

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__getitem__(index)[source]
self[index]:

Return the list value at the index position.

Raises

IndexError – Index out of range.

__gt__(other)[source]

self > other: Return a boolean indicating whether the list is greater than the other list.

The return value indicates whether the original list is greater than the other list (or in case of a ListView, its original list), based on the lexicographical ordering Python defines for sequence types (see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types)

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__iter__()[source]

Return an iterator through the list items.

__le__(other)[source]

self < other: Return a boolean indicating whether the list is less than or equal to the other list.

The return value indicates whether the original list is less than or equal to the other list (or in case of a ListView, its original list), based on the lexicographical ordering Python defines for sequence types (see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types)

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__len__()[source]

len(self): Return the number of items in the list.

The return value is the number of items in the original list.

__lt__(other)[source]

self < other: Return a boolean indicating whether the list is less than the other list.

The return value indicates whether the original list is less than the other list (or in case of a ListView, its original list), based on the lexicographical ordering Python defines for sequence types (see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types)

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__mul__(number)[source]

self * number: Return a new view on the multiplication of the list with a number.

The returned ListView object is a view on a new list object of the type of the left hand operand that contains the items that are in the original list of the left hand operand as many times as specified by the right hand operand.

A number <= 0 causes the returned list to be empty.

The left hand operand is not changed.

__ne__(other)[source]

self != other: Return a boolean indicating whether the list is not equal to the other list.

The return value indicates whether the items in the original list are not equal to the items in the other list (or in case of a ListView, its original list).

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__repr__()[source]

repr(self): Return a string representation of the view suitable for debugging.

The original list is represented using its repr() representation.

__reversed__()[source]

reversed(self) ...: Return an iterator through the list in reversed iteration order.

The returned iterator yields the items in the original list in the reversed iteration order.

__rmul__(number)[source]

number * self: Return a new view on the multiplication of the list with a number.

This method is a fallback and is called only if the left operand does not support the operation.

The returned ListView object is a view on a new list object of the type of the right hand operand that contains the items that are in the original list of the right hand operand as many times as specified by the left hand operand.

A number <= 0 causes the returned list to be empty.

The right hand operand is not changed.

copy()[source]

Return a new view on a shallow copy of the list.

The returned ListView object is a new view object on a list object of the type of the original list.

If the list type is immutable, the returned list object may be the original list object. If the list type is mutable, the returned list is a new list object that is a shallow copy of the original list object.

count(value)[source]

Return the number of times the specified value occurs in the list.

index(value, start=0, stop=9223372036854775807)[source]

Return the index of the first item in the list with the specified value.

The search is limited to the index range defined by the specified start and stop parameters, whereby stop is the index of the first item after the search range.

Raises

ValueError – No such item is found.