PlutusList

class cometa.plutus_data.plutus_list.PlutusList(ptr=None)[source]

Bases: Sequence[PlutusData]

Represents a list of Plutus data elements.

This class provides Pythonic list-like operations for working with Plutus data lists on the Cardano blockchain. It supports native Python types (int, str, bytes) which are automatically converted to PlutusData.

Example

>>> plist = PlutusList()
>>> plist.append(42)           # int -> PlutusData
>>> plist.append("hello")      # str -> PlutusData (UTF-8 bytes)
>>> plist.append(b"\x01\x02") # bytes -> PlutusData
>>> len(plist)
3
>>> plist[0].to_int()
42
>>> # Iteration
>>> for item in plist:
...     print(item.kind)
>>> # List-like operations
>>> plist += [1, 2, 3]
>>> 42 in plist  # Note: checks PlutusData equality
__init__(ptr=None)[source]

Initializes a new PlutusList instance.

Parameters:

ptr – Optional internal pointer for initialization from C library.

Raises:

CardanoError – If creation fails or pointer is invalid.

Return type:

None

__enter__()[source]

Enables use of PlutusList as a context manager.

Return type:

PlutusList

__exit__(exc_type, exc_val, exc_tb)[source]

Exits the context manager.

Return type:

None

__repr__()[source]

Returns a string representation of the PlutusList.

Return type:

str

__len__()[source]

Returns the number of elements in the list.

Return type:

int

__bool__()[source]

Returns True if the list is not empty.

Return type:

bool

__eq__(other)[source]

Checks equality with another PlutusList.

Parameters:

other (object)

Return type:

bool

__getitem__(index: int) PlutusData[source]
__getitem__(index: slice) list[PlutusData]

Gets an element or slice from the list.

Parameters:

index – An integer index or slice.

Returns:

A single PlutusData if index is an int, or a list of PlutusData if slice.

Raises:

IndexError – If index is out of bounds.

__iter__()[source]

Iterates over all elements in the list.

Return type:

Iterator[‘PlutusData’]

__contains__(item)[source]

Checks if an item is in the list.

Parameters:

item (PlutusDataLike) – A PlutusData or native Python type (int, str, bytes).

Returns:

True if the item is found in the list.

Return type:

bool

__add__(other)[source]

Concatenates this list with another list or iterable.

Parameters:

other (PlutusList | Iterable) – Another PlutusList or iterable of PlutusData/native types.

Returns:

A new PlutusList containing all elements from both lists.

Return type:

PlutusList

classmethod from_list(elements)[source]

Creates a PlutusList from an iterable of values.

Parameters:

elements (Iterable['PlutusDataLike']) – An iterable of PlutusData objects or Python primitives (int, str, bytes).

Returns:

A new PlutusList containing all the elements.

Raises:

CardanoError – If creation fails.

Return type:

PlutusList

classmethod from_cbor(reader)[source]

Deserializes a PlutusList from CBOR data.

Parameters:

reader (CborReader) – A CborReader positioned at the list data.

Returns:

A new PlutusList deserialized from the CBOR data.

Raises:

CardanoError – If deserialization fails.

Return type:

PlutusList

to_cbor(writer)[source]

Serializes the list to CBOR format.

Parameters:

writer (CborWriter) – A CborWriter to write the serialized data to.

Raises:

CardanoError – If serialization fails.

Return type:

None

clear_cbor_cache()[source]

Clears the cached CBOR representation.

Warning

Clearing the CBOR cache may change the binary representation when serialized, which can alter the data and invalidate existing signatures.

Return type:

None

to_cip116_json(writer)[source]

Serializes this object to CIP-116 compliant JSON.

Parameters:

writer (JsonWriter) – The JsonWriter to write the JSON to.

Raises:

CardanoError – If serialization fails.

Return type:

None

add(element)[source]

Adds a PlutusData element to the end of the list.

Parameters:

element (PlutusData) – The PlutusData to add.

Raises:

CardanoError – If the operation fails.

Return type:

None

append(value)[source]

Appends a value to the end of the list.

This method accepts native Python types and converts them to PlutusData: - int -> Integer PlutusData - str -> Bytes PlutusData (UTF-8 encoded) - bytes -> Bytes PlutusData - PlutusData -> Used directly

Parameters:

value (PlutusDataLike) – The value to append.

Raises:
  • CardanoError – If the operation fails.

  • TypeError – If the value type is not supported.

Return type:

None

extend(values)[source]

Extends the list with multiple values.

Parameters:

values (Iterable['PlutusDataLike']) – An iterable of values to append.

Return type:

None

get(index)[source]

Retrieves the element at a specific index.

Parameters:

index (int) – The index of the element to retrieve.

Returns:

The PlutusData at the specified index.

Raises:
  • CardanoError – If retrieval fails.

  • IndexError – If index is out of bounds.

Return type:

PlutusData

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

Returns the index of the first occurrence of a value.

Parameters:
  • value (PlutusDataLike) – The value to search for.

  • start (int) – Start searching from this index.

  • stop (int) – Stop searching at this index.

Returns:

The index of the first occurrence.

Raises:

ValueError – If the value is not found.

Return type:

int

count(value)[source]

Returns the number of occurrences of a value.

Parameters:

value (PlutusDataLike) – The value to count.

Returns:

The number of occurrences.

Return type:

int

copy()[source]

Creates a shallow copy of this list.

Returns:

A new PlutusList with the same elements.

Return type:

PlutusList