w3resource

Python: Clone or copy a list

Python List: Exercise-9 with Solution

Clone or Copy a List

Write a Python program to clone or copy a list.

Visual Presentation:

Python: Clone or copy a list

Sample Solution:

Python Code:

# Create a list 'original_list' with some elements
original_list = [10, 22, 44, 23, 4]

# Create a new list 'new_list' and copy the elements from 'original_list' into it
new_list = list(original_list)

# Print the contents of the 'original_list'
print(original_list)

# Print the contents of the 'new_list', which is a copy of 'original_list'
print(new_list)

Sample Output:

[10, 22, 44, 23, 4]                                                                                           
[10, 22, 44, 23, 4]

Explanation:

In the above exercise -

original_list = [10, 22, 44, 23, 4]  -> This line creates a new list called original_list that contains five integer values.

new_list = list(original_list)  -> This line creates a new list called new_list that is a copy of the original_list. The list function is called with original_list as its argument, which creates a new list object with the same elements as original_list.

Flowchart:

Flowchart: Clone or copy a list

Python Code Editor:

Previous: Write a Python program to check a list is empty or not.
Next: Write a Python program to find the list of words that are longer than n from a given list of words.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-9.php