YouTip LogoYouTip

Python3 Att Dictionary Values

# Python3.x Python3 Dictionary values() Method [![Image 3: Python3 Dictionary](#) Python3 Dictionary](#) * * * ## Description The Python3 dictionary values() method returns a view object. [dict.keys()](#), dict.values(), and [dict.items()](#) all return view objects, which provide a dynamic view of the dictionary's entities. This means that when the dictionary changes, the view changes accordingly. View objects are not lists and do not support indexing. You can use list() to convert them to lists. We cannot make any modifications to view objects because dictionary view objects are read-only. ## Syntax The syntax for the values() method is: dict.values() ## Parameters * NA. ## Return Value Returns a view object. ## Example The following example demonstrates the usage of the values() method: ## Example >>> dishes ={'eggs': 2,'sausage': 1,'bacon': 1,'spam': 500} >>> keys = dishes.keys() >>> values = dishes.values() >>># Iterate >>> n =0 >>>for val in values: ... n += val >>>print(n) 504 >>># keys and values iterate in the same order (insertion order) >>>list(keys)# Use list() to convert to a list ['eggs','sausage','bacon','spam'] >>>list(values) [2,1,1,500] >>># The view object is dynamic and affected by changes to the dictionary. The following deletes keys from the dictionary, and the view object changes accordingly when converted to a list >>>del dishes['eggs'] >>>del dishes['sausage'] >>>list(values) [1,500] >>># Comparing two dict.values() always returns False >>> d ={'a': 1} >>> d.values()== d.values() False * * Python3 Dictionary](#)
← Python3 File ClosePython3 Att Dictionary Values β†’