A sorteddict provides the same methods as a dict. Additionally, a sorteddict efficiently maintains its keys in sorted order. Consequently, the keys method will return the keys in sorted order, the popitem method will remove the item with the lowest key, etc.
key specifies a function of one argument that is used to extract a comparison sort key from each dict key, e.g. sorteddict(str.lower). If no function is specified, the default compares the elements directly. The key function must always return the same key for an item or the results are unpredictable.
An optional iterable provides an initial series of items to populate the sorteddict. Each item in the series must itself contain to items. The first is used as a key in the new dictionary, and the second as the key’s value. If a given key is seen more than once, the last value associated with it is retained in the new dictionary.
If keyword arguments are given, the keywords themselves with their associated values are added as items to the dictionary. If a key is specified both in the positional argument and as a keyword argument, the value associated with the keyword is retained in the dictionary. For example, these all return a dictionary equal to {"one": 2, "two": 3}:
The first example only works for keys that are valid Python identifiers; the others work with any valid keys.
Returns True if and only if x is a key in the dictionary.
Requires operations in the average case.
Return type: | bool |
---|
Remove d[key] from d. Raises a KeyError if key is not in the dictionary.
Requires comparisons.
Return the item of d with key key. Raises a KeyError if key is not in the dictionary.
If a subclass of dict defines a method __missing__(), if the key key is not present, the d[key] operation calls that method with the key key as argument. The d[key] operation then returns or raises whatever is returned or raised by the __missing__(key) call if the key is not present. No other operations or methods invoke __missing__(). If __missing__() is not defined, KeyError is raised. __missing__() must be a method; it cannot be an instance variable.
Requires operations in the average case.
Return type: | value |
---|
Test two dictionaries for equality (or inequality). Dictionaries compare equal if and only if they have the same length, if all of the keys of L may be found in L2, and all of the corresponding values compare equal.
In the worst case, requires operations and comparisons.
Return type: | bool |
---|
Creates an iterator over the sorted keys of the dictionary.
Requires operations to create the iterator. Each element from the iterator requires operations to retrieve, or operations to iterate over the entire dictionary.
Return type: | iterator |
---|
Returns the number of (key, value) pairs in the dictionary.
Requires operations.
Return type: | int |
---|
Sets d[key] to value.
In the average case, requires operations and comparisons.
Remove all elements from the dictionary.
Requires total operations in the worst case.
Creates a shallow copy of the dictionary.
Requires total operations and no comparisons.
Return type: | sorteddict |
---|
Create a new dictionary with keys from seq and values set to value. key specifies a function of one argument that is used to extract a comparison sort key from each dict key.
fromkeys() is a class method that returns a new dictionary. value defaults to None.
Return type: | sorteddict |
---|
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.
Requires total operations in the average case.
Return type: | value |
---|
In Python 2, returns a blist of the dictionary’s items ((key, value) pairs).
In Python 3, returns a new ItemsView of the dictionary’s items. In addition to the methods provided by the built-in view, the ItemsView is indexable (e.g., d.items()[5]).
Requires operations.
Return type: | blist or ItemsView |
---|
In Python 2, returns a sortedset of the dictionary’s keys.
In Python 3, returns a new KeysView of the dictionary’s keys. In addition to the methods provided by the built-in view, the KeysView is indexable (e.g., d.keys()[5]).
Requires operations.
Return type: | sortedset or KeysView |
---|
If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.
Requires operations if key is not in the dictionary. Otherwise, requires comparisons.
Return type: | value |
---|
Remove and return the (key, value) pair with the least key from the dictionary.
If the dictionary is empty, calling popitem() raises a KeyError.
Requires operations and no comparisons.
Return type: | (key, value) tuple |
---|
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
Requires operations if key is already in the dictionary. Otherwise, requires comparisons.
Update the dictionary with the key/value pairs from other, overwriting existing keys.
update() accepts either another dictionary object or an iterable of key/value pairs (as a tuple or other iterable of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: d.update(red=1, blue=2).
In the average case, requires operations and comparisons, where m is the combined size of all the other sets and n is the initial size of d.
In Python 2, returns a blist of the dictionary’s values.
In Python 3, returns a new ValuesView of the dictionary’s values. In addition to the methods provided by the built-in view, the ValuesView is indexable (e.g., d.values()[5]).
Requires operations.
Return type: | blist or ValuesView |
---|
A KeysView object is a dynamic view of the dictionary’s keys, which means that when the dictionary’s keys change, the view reflects those changes.
The KeysView class implements the Set and Sequence Abstract Base Classes.
Returns the number of entries in the dictionary.
Requires operations.
Return type: | int |
---|
Returns an iterator over the keys in the dictionary. Keys are iterated over in their sorted order.
Iterating views while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate over all entries.
Requires operations to create the iterator. Each element from the iterator requires operations to retrieve, or operations to iterate over the entire dictionary.
Return type: | iterator |
---|
Returns True iff x is one of the underlying dictionary’s keys.
Requires operations.
Return type: | bool |
---|
Returns the key at position i.
Requires operations if the dictionary’s size has not been changed recently. Otherwise, requires operations.
Return type: | value |
---|
Returns the intersection of the keys and the other object as a new set.
Requires operations and comparisons, where m is the size of other and n is the size of keysview.
Return type: | sortedset |
---|
Returns the union of the keys and the other object as a new set.
Requires operations or comparisons, where m is the size of other and n is the size of S.
Return type: | sortedset |
---|
Return the difference between the keys and the other object (all keys that aren’t in other) as a new set.
Requires operations and comparisons, where m is the size of other and n is the size of keysview.
Return type: | sortedset |
---|
Return the symmetric difference (all elements either in the keys or other, but not in both) of the keys and the other object as a new set.
Requires operations and comparisons, where m is the size of other and n is the size of keysview.
Return type: | sortedset |
---|
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.
Returns the number of occurrences of key in the set.
Requires operations.
Return type: | int |
---|
Returns the smallest k such that and . Raises KeyError if key is not present. stop defaults to the end of the set. start defaults to the beginning. Negative indexes are supported, as for slice indices.
Requires operations.
Return type: | int |
---|
A ValuesView object is a dynamic view of the dictionary’s values, which means that when the dictionary’s values change, the view reflects those changes.
The ValuesView class implements the Sequence Abstract Base Class.
Returns the number of entries in the dictionary.
Requires operations.
Return type: | int |
---|
Returns an iterator over the values in the dictionary. Values are iterated over in sorted order of the keys.
Iterating views while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate over all entries.
Requires operations to create the iterator. Each element from the iterator requires operations to retrieve, or operations to iterate over the entire dictionary.
Return type: | iterator |
---|
Returns True iff x is one of the underlying dictionary’s values.
Requires operations.
Return type: | bool |
---|
Returns the value at position i.
Requires operations if the dictionary’s size has not been changed recently. Otherwise, requires operations.
Return type: | value |
---|
Returns the number of occurrences of value in the set.
Requires operations and comparisons in the worst case.
Return type: | int |
---|
Returns the smallest k such that and . Raises KeyError if value is not present. stop defaults to the end of the set. start defaults to the beginning. Negative indexes are supported, as for slice indices.
In the worst case, requires operations and comparisons.
Return type: | int |
---|
A ItemsView object is a dynamic view of the dictionary’s (key, value) pairs, which means that when the dictionary changes, the view reflects those changes.
The ItemsView class implements the Set and Sequence Abstract Base Classes. However, the set-like operations (&, |, -, ^) will only operate correctly if all of the dictionary’s values are hashable.
Returns the number of entries in the dictionary.
Requires operations.
Return type: | int |
---|
Returns an iterator over the items in the dictionary. Items are iterated over in sorted order of the keys.
Iterating views while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate over all entries.
Requires operations to create the iterator. Each element from the iterator requires operations to retrieve, or operations to iterate over the entire dictionary.
Return type: | iterator |
---|
Returns True iff x is one of the underlying dictionary’s items.
Requires operations.
Return type: | bool |
---|
Returns the (key, value) pair at position i.
Requires operations if the dictionary’s size has not been changed recently. Otherwise, requires operations.
Return type: | item |
---|
Returns the intersection of the items and the other object as a new set.
Requires operations and comparisons, where m is the size of other and n is the size of itemsview.
Return type: | sortedset |
---|
Returns the union of the items and the other object as a new set.
Requires operations or comparisons, where m is the size of other and n is the size of S.
Return type: | sortedset |
---|
Return the difference between the items and the other object (all items that aren’t in other) as a new set.
Requires operations and comparisons, where m is the size of other and n is the size of itemsview.
Return type: | sortedset |
---|
Return the symmetric difference (all elements either in the items or other, but not in both) of the items and the other object as a new set.
Requires operations and comparisons, where m is the size of other and n is the size of itemsview.
Return type: | sortedset |
---|
Returns the number of occurrences of item in the set.
Requires operations.
Return type: | int |
---|
Returns the smallest k such that and . Raises KeyError if item is not present. stop defaults to the end of the set. start defaults to the beginning. Negative indexes are supported, as for slice indices.
Requires operations.
Return type: | int |
---|