The reversed function of dictionary views

The reversed() function inverses the order of a dictionary view instance. The dictionary view could be of any type among dict_keys, dict_values and dict_items.

Dictionary View Updated 22 Nov 2020 1 min read

Function Name

reversed

Function Signature

reversed(dictionaryViewObject)

Overview

Return Value

A dictionary view object - An object of type dict_keys, dict_items or dict_values

Example

python
# Example python program that reverses a dictionary view

            halogensVsAtomicNumber = {"Fluorine": 9,

                                      "Chlorine": 17,

                                      "Bromine": 35,

                                      "Iodine": 53,

                                      "Astatine": 85

                                      };

# Get the dictionary view of type dict_items

            itemView = halogensVsAtomicNumber.items();

# Reverse the arrangement

            reversedView = reversed(itemView);

# Print the elements from the reversed view

            print("Dictionary view in reverse order:");

            for item in reversedView:

                print(item);

Output

Output
Dictionary view in reverse order:

            ('Astatine', 85)

            ('Iodine', 53)

            ('Bromine', 35)

            ('Chlorine', 17)

            ('Fluorine', 9)