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 Lists

      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 Lists

      In Python, a variable is just a label or reference to the object in the memory. Hence, the assignment "lst1 = lst" refers to the same list object in the memory. Take a look at the following example
      lst = [10, 20]
      print ("lst:", lst, "id(lst):",id(lst))
      lst1 = lst
      print ("lst1:", lst1, "id(lst1):",id(lst1))
      It will produce the following output
      lst: [10, 20] id(lst): 1677677188288
      lst1: [10, 20] id(lst1): 1677677188288
      As a result, if we update "lst", it will automatically reflect in "lst1". Change lst[0] to 100
      lst[0]=100
      print ("lst:", lst, "id(lst):",id(lst))
      print ("lst1:", lst1, "id(lst1):",id(lst1))
      It will produce the following output
      lst: [100, 20] id(lst): 1677677188288
      lst1: [100, 20] id(lst1): 1677677188288
      Hence, we can say that "lst1" is not the physical copy of "lst".

      Using the Copy Method of List Class

      Python's list class has a copy() method to create a new physical copy of a list object.

      Syntax

      lst1 = lst.copy()
      The new list object will have a different id() value. The following example demonstrates this
      lst = [10, 20]
      lst1 = lst.copy()
      print ("lst:", lst, "id(lst):",id(lst))
      print ("lst1:", lst1, "id(lst1):",id(lst1))
      It will produce the following output
      lst: [10, 20] id(lst): 1677678705472
      lst1: [10, 20] id(lst1): 1677678706304
      Even if the two lists have same data, they have different id() value, hence they are two different objects and "lst1" is a copy of "lst".
      If we try to modify "lst", it will not reflect in "lst1". See the following example
      lst[0]=100
      print ("lst:", lst, "id(lst):",id(lst))
      print ("lst1:", lst1, "id(lst1):",id(lst1))
      It will produce the following output
      lst: [100, 20] id(lst): 1677678705472
      lst1: [10, 20] id(lst1): 1677678706304

      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.