How to Copy List Element to Other Variable in Python

In this tutorial, learn how to copy list elements to another variable using Python. The short answer is: use the copy() to clone the list element to other list variables. You can create a clone of the list and get the list elements from the new variable.

You can also copy all the elements from one list to other. Check each examples showing the method to copy the list elements. Learn each method one by one and execute the examples to get output.

Clone or Copy List Element to Other Using Python Copy()

If you want to copy the list to other Python variable. You have to use the Python copy(). To use the copy() function, you have to use the list variable followed by the function. Now, assign this to the list variable where you want to copy this list.

Output

[‘one’, ‘two’, 37, ‘four’, 51]

The above example prints the new list elements which are the exact copy of the old list. It copied all the elements of the first list to the second list.

You can also assign one list element to the other list element. To perform this task using Python, you have to read further.

Assign One List to Other For Cloning in Python

You can also assign one list elements to the other variable in Python. To perform this task, you have to equate the old list to the new list. This clone the old list elements to the new list elements.

This works same as the above-given example but it assigns the list to a new variable.

Output

[‘one’, ‘two’, 37, ‘four’, 51]

The above example showing the list elements of the new list variable. Check the above example assigning old variable to the new variable. It prints the output using the Python print statement.

Copy Elements of One List to Other Using Python Extend()

In addition to above all, you can also add the elements of one list to the other list. To perform this task, you have to use the Python extend() and pass the list variable as the argument.

Use the below-given example to extend one list to the other list. You may also like to read how to append or insert elements to list in Python

Output

[‘one’, ‘two’, 37, ‘four’, 51, ‘third’, ‘forth’, 21]

The above example showing the list elements after the addition. You can say this list as the copied list of the other including old elements. This is the combined elements of the two list in Python.

You may also like to read

Hope, you like this tutorial of how to copy one list to the other using Python. If you have any query regarding the tutorial, please comment below.

References

Also tell me, which method you are using to clone the list in Python.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.