Sorted items view is a dynamic view of the sorted dict's items. When the sorted dict's items change, the view reflects those changes. The items view implements the set and sequence abstract base classes.
| 704 | |
| 705 | |
| 706 | class SortedItemsView(ItemsView, Sequence): |
| 707 | """Sorted items view is a dynamic view of the sorted dict's items. |
| 708 | |
| 709 | When the sorted dict's items change, the view reflects those changes. |
| 710 | |
| 711 | The items view implements the set and sequence abstract base classes. |
| 712 | |
| 713 | """ |
| 714 | __slots__ = () |
| 715 | |
| 716 | |
| 717 | @classmethod |
| 718 | def _from_iterable(cls, it): |
| 719 | return SortedSet(it) |
| 720 | |
| 721 | |
| 722 | def __getitem__(self, index): |
| 723 | """Lookup item at `index` in sorted items view. |
| 724 | |
| 725 | ``siv.__getitem__(index)`` <==> ``siv[index]`` |
| 726 | |
| 727 | Supports slicing. |
| 728 | |
| 729 | Runtime complexity: `O(log(n))` -- approximate. |
| 730 | |
| 731 | >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3}) |
| 732 | >>> siv = sd.items() |
| 733 | >>> siv[0] |
| 734 | ('a', 1) |
| 735 | >>> siv[-1] |
| 736 | ('c', 3) |
| 737 | >>> siv[:] |
| 738 | [('a', 1), ('b', 2), ('c', 3)] |
| 739 | >>> siv[100] |
| 740 | Traceback (most recent call last): |
| 741 | ... |
| 742 | IndexError: list index out of range |
| 743 | |
| 744 | :param index: integer or slice for indexing |
| 745 | :return: item or list of items |
| 746 | :raises IndexError: if index out of range |
| 747 | |
| 748 | """ |
| 749 | _mapping = self._mapping |
| 750 | _mapping_list = _mapping._list |
| 751 | |
| 752 | if isinstance(index, slice): |
| 753 | keys = _mapping_list[index] |
| 754 | return [(key, _mapping[key]) for key in keys] |
| 755 | |
| 756 | key = _mapping_list[index] |
| 757 | return key, _mapping[key] |
| 758 | |
| 759 | |
| 760 | __delitem__ = _view_delitem |
| 761 | |
| 762 | |
| 763 | class SortedValuesView(ValuesView, Sequence): |