pub trait PyListMethods<'py>: Sealed {
Show 19 methods // Required methods fn len(&self) -> usize; fn is_empty(&self) -> bool; fn as_sequence(&self) -> &Bound<'py, PySequence>; fn into_sequence(self) -> Bound<'py, PySequence>; fn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>>; unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>; fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyList>; fn set_item<I>(&self, index: usize, item: I) -> PyResult<()> where I: ToPyObject; fn del_item(&self, index: usize) -> PyResult<()>; fn set_slice( &self, low: usize, high: usize, seq: &Bound<'_, PyAny> ) -> PyResult<()>; fn del_slice(&self, low: usize, high: usize) -> PyResult<()>; fn append<I>(&self, item: I) -> PyResult<()> where I: ToPyObject; fn insert<I>(&self, index: usize, item: I) -> PyResult<()> where I: ToPyObject; fn contains<V>(&self, value: V) -> PyResult<bool> where V: ToPyObject; fn index<V>(&self, value: V) -> PyResult<usize> where V: ToPyObject; fn iter(&self) -> BoundListIterator<'py> ; fn sort(&self) -> PyResult<()>; fn reverse(&self) -> PyResult<()>; fn to_tuple(&self) -> Bound<'py, PyTuple>;
}
Expand description

Implementation of functionality for PyList.

These methods are defined for the Bound<'py, PyList> smart pointer, so to use method call syntax these methods are separated into a trait, because stable Rust does not yet support arbitrary_self_types.

Required Methods§

source

fn len(&self) -> usize

Returns the length of the list.

source

fn is_empty(&self) -> bool

Checks if the list is empty.

source

fn as_sequence(&self) -> &Bound<'py, PySequence>

Returns self cast as a PySequence.

source

fn into_sequence(self) -> Bound<'py, PySequence>

Returns self cast as a PySequence.

source

fn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>>

Gets the list item at the specified index.

§Example
use pyo3::{prelude::*, types::PyList};
Python::with_gil(|py| {
    let list = PyList::new_bound(py, [2, 3, 5, 7]);
    let obj = list.get_item(0);
    assert_eq!(obj.unwrap().extract::<i32>().unwrap(), 2);
});
source

unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>

Gets the list item at the specified index. Undefined behavior on bad index. Use with caution.

§Safety

Caller must verify that the index is within the bounds of the list.

source

fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyList>

Takes the slice self[low:high] and returns it as a new list.

Indices must be nonnegative, and out-of-range indices are clipped to self.len().

source

fn set_item<I>(&self, index: usize, item: I) -> PyResult<()>
where I: ToPyObject,

Sets the item at the specified index.

Raises IndexError if the index is out of range.

source

fn del_item(&self, index: usize) -> PyResult<()>

Deletes the indexth element of self.

This is equivalent to the Python statement del self[i].

source

fn set_slice( &self, low: usize, high: usize, seq: &Bound<'_, PyAny> ) -> PyResult<()>

Assigns the sequence seq to the slice of self from low to high.

This is equivalent to the Python statement self[low:high] = v.

source

fn del_slice(&self, low: usize, high: usize) -> PyResult<()>

Deletes the slice from low to high from self.

This is equivalent to the Python statement del self[low:high].

source

fn append<I>(&self, item: I) -> PyResult<()>
where I: ToPyObject,

Appends an item to the list.

source

fn insert<I>(&self, index: usize, item: I) -> PyResult<()>
where I: ToPyObject,

Inserts an item at the specified index.

If index >= self.len(), inserts at the end.

source

fn contains<V>(&self, value: V) -> PyResult<bool>
where V: ToPyObject,

Determines if self contains value.

This is equivalent to the Python expression value in self.

source

fn index<V>(&self, value: V) -> PyResult<usize>
where V: ToPyObject,

Returns the first index i for which self[i] == value.

This is equivalent to the Python expression self.index(value).

source

fn iter(&self) -> BoundListIterator<'py>

Returns an iterator over this list’s items.

source

fn sort(&self) -> PyResult<()>

Sorts the list in-place. Equivalent to the Python expression l.sort().

source

fn reverse(&self) -> PyResult<()>

Reverses the list in-place. Equivalent to the Python expression l.reverse().

source

fn to_tuple(&self) -> Bound<'py, PyTuple>

Return a new tuple containing the contents of the list; equivalent to the Python expression tuple(list).

This method is equivalent to self.as_sequence().to_tuple() and faster than PyTuple::new(py, this_list).

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'py> PyListMethods<'py> for Bound<'py, PyList>