Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Dictionary View Objects

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Dictionary View Objects

      The items(), keys() and values() methods of dict class return view objects. These views are refreshed dynamically whenever any change occurs in the contents of their source dictionary object.

      items() Method

      The items() method returns a dict_items view object. It contains a list of tuples, each tuple made up of respective key, value pairs.

      Syntax

      Obj = dict.items()

      Return value

      The items() method returns dict_items object which is a dynamic view of (key,value) tuples.

      Example

      In the following example, we first obtain the dict_items() object with items() method and check how it is dynamically updated when the dictionary object is updated.
      numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
      obj = numbers.items()
      print ('type of obj: ', type(obj))
      print (obj)
      print ("update numbers dictionary")
      numbers.update({50:"Fifty"})
      print ("View automatically updated")
      print (obj)
      It will produce the following output
      type of obj: <class 'dict_items'>
      dict_items([(10, 'Ten'), (20, 'Twenty'), (30, 'Thirty'), (40, 'Forty')])
      update numbers dictionary
      View automatically updated
      dict_items([(10, 'Ten'), (20, 'Twenty'), (30, 'Thirty'), (40, 'Forty'), (50, 'Fifty')])

      keys() Method

      The keys() method of dict class returns dict_keys object which is a list of all keys defined in the dictionary. It is a view object, as it gets automatically updated whenever any update action is done on the dictionary object

      Syntax

      Obj = dict.keys()

      Return value

      The keys() method returns dict_keys object which is a view of keys in the dictionary.

      Example

      numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
      obj = numbers.keys()
      print ('type of obj: ', type(obj))
      print (obj)
      print ("update numbers dictionary")
      numbers.update({50:"Fifty"})
      print ("View automatically updated")
      print (obj)
      It will produce the following output 
      type of obj: <class 'dict_keys'>
      dict_keys([10, 20, 30, 40])
      update numbers dictionary
      View automatically updated
      dict_keys([10, 20, 30, 40, 50])

      values() Method

      The values() method returns a view of all the values present in the dictionary. The object is of dict_value type, which gets automatically updated.

      Syntax

      Obj = dict.values()

      Return value

      The values() method returns a dict_values view of all the values present in the dictionary.

      Example

      numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
      obj = numbers.values()
      print ('type of obj: ', type(obj))
      print (obj)
      print ("update numbers dictionary")
      numbers.update({50:"Fifty"})
      print ("View automatically updated")
      print (obj)
      It will produce the following output
      type of obj: <class 'dict_values'>
      dict_values(['Ten', 'Twenty', 'Thirty', 'Forty'])
      update numbers dictionary
      View automatically updated
      dict_values(['Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty'])

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.