Copying Lists in Python: Duplicating List Contents

thumb_up 1  ·  sell Python list copying, Creating copies of Python lists, Duplicating list contents in Python

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



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back