A sortedlist provides most of the same methods as a blist (and, hence, a list), but keeps the items in sorted order. Methods that would insert an item at a particular location are not included (e.g., append, insert). To add an element to the sortedlist, use add. To add several elements, use update. To remove an element, use discard, remove, or del L[i].
An optional iterable provides an initial series of items to populate the sortedlist.
key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly). The key function must always return the same key for an item or the results are unpredictable.
A sortedlist can be used as an order statistic tree (Cormen et al., Introduction to Algorithms, ch. 14) that allows duplicate keys.
Returns True if and only if x is an element in the list.
Requires total operations or comparisons.
Return type: | bool |
---|
Removes the element located at index i from the list.
Requires operations and no comparisons.
Removes the elements from i to j from the list.
Requires operations and no comparisons.
Compares two lists. For full details see Comparisons in the Python language reference.
In the worst case, requires operations and comparisons.
Return type: | bool |
---|
Returns the element at position i.
Requires operations in the worst case but only operations if the list’s size has not been changed recently. Requires no comparisons in any case.
(Cormen et al. call this operation “SELECT”.)
Return type: | item |
---|
Returns a new sortedlist containing the elements from i to j.
Requires operations and no comparisons.
Return type: | sortedlist |
---|
Increase the length of the list by a factor of k, by inserting k-1 additional shallow copies of each item in the list.
Requires operations and no comparisons.
Creates an iterator over the list.
Requires operations to create the iterator. Each element from the iterator requires operations to retrieve, or operations to iterate over the entire list.
Return type: | iterator |
---|
Returns the number of elements in the list.
Requires operations.
Return type: | int |
---|
Returns a new sorted list containing k shallow copies of each item in L.
Requires operations and no comparisons.
Return type: | sortedlist |
---|
Creates an iterator to traverse the list in reverse.
Requires operations to create the iterator. Each element from the iterator requires operations to retrieve, or operations to iterate over the entire list. Requires no comparisons in any case.
Return type: | iterator |
---|
Replace the item at position i of L with x.
Requires operations in the worst case but only operations if the list’s size has not been changed recently. Requires no comparisons in any case.
Replace the items at positions i through j with the contents of iterable.
Requires operations and no comparisons.
Add the element value to the list.
Requires total operations or comparisons.
Similarly to the bisect module in the standard library, this returns an appropriate index to insert value in L. If value is already present in L, the insertion point will be before (to the left of) any existing entries.
Requires total operations or comparisons.
Same as bisect_left.
Same thing as bisect_left, but if value is already present in L, the insertion point will be after (to the right of) any existing entries.
Returns the number of occurrences of value in the list.
Requires operations and comparisons in the worst case.
Return type: | int |
---|
Removes the first occurrence of value. If value is not a member, does nothing.
In the worst case, requires operations and comparisons.
Returns the smallest k such that and . Raises ValueError if value is not present. stop defaults to the end of the list. start defaults to the beginning. Negative indexes are supported, as for slice indices.
In the worst case, requires operations and comparisons, where m is stop - start.
(Cormen et al. call this operation “RANK”.)
Return type: | int |
---|
Removes and return item at index (default last). Raises IndexError if list is empty or index is out of range. Negative indexes are supported, as for slice indices.
Requires operations and no comparisons.
Return type: | item |
---|
Remove first occurrence of value. Raises ValueError if value is not present.
In the worst case, requires operations and comparisons.
Grow the list by inserting all elements from the iterable.
Requires operations and comparisons, where m is the size of the iterable and n is the size of the list initially.