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
      Copy Dictionaries

      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.

      Copy Dictionaries

      Since a variable in Python is merely a label or reference to an object in the memory, a simple assignment operator will not create copy of object.

      Example 1

      In this example, we have a dictionary "d1" and we assign it to another variable "d2". If "d1" is updated, the changes also reflect in "d2".
      d1 = {"a":11, "b":22, "c":33}
      d2 = d1
      print ("id:", id(d1), "dict: ",d1)
      print ("id:", id(d2), "dict: ",d2)
      
      d1["b"] = 100
      print ("id:", id(d1), "dict: ",d1)
      print ("id:", id(d2), "dict: ",d2)

      Output

      id: 2215278891200 dict: {'a': 11, 'b': 22, 'c': 33}
      id: 2215278891200 dict: {'a': 11, 'b': 22, 'c': 33}
      id: 2215278891200 dict: {'a': 11, 'b': 100, 'c': 33}
      id: 2215278891200 dict: {'a': 11, 'b': 100, 'c': 33}
      To avoid this, and make a shallow copy of a dictionary, use the copy() method instead of assignment.

      Example 2

      d1 = {"a":11, "b":22, "c":33}
      d2 = d1.copy()
      print ("id:", id(d1), "dict: ",d1)
      print ("id:", id(d2), "dict: ",d2)
      d1["b"] = 100
      print ("id:", id(d1), "dict: ",d1)
      print ("id:", id(d2), "dict: ",d2)

      Output

      When "d1" is updated, "d2" will not change now because "d2" is the copy of dictionary object, not merely a reference.
      id: 1586671734976 dict: {'a': 11, 'b': 22, 'c': 33}
      id: 1586673973632 dict: {'a': 11, 'b': 22, 'c': 33}
      id: 1586671734976 dict: {'a': 11, 'b': 100, 'c': 33}
      id: 1586673973632 dict: {'a': 11, 'b': 22, 'c': 33}

      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.