The Reversed Function Of Dictionary Views

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:

# 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:

Dictionary view in reverse order:
('Astatine', 85)
('Iodine', 53)
('Bromine', 35)
('Chlorine', 17)
('Fluorine', 9)

 


Copyright 2023 © pythontic.com