When it is required to copy all the elements from one array to another, an empty array with ‘None’ elements is created. A simple for loop is used to iterate over the elements, and the ‘=’ operator is used to assign values to the new list.
Below is a demonstration of the same −
Example
my_list_1 = [34, 56, 78, 90, 11, 23] my_list_2 = [None] * len(my_list_1) for i in range(0, len(my_list_1)): my_list_2[i] = my_list_1[i] print("The list is : ") for i in range(0, len(my_list_1)): print(my_list_1[i]) print() print("The new list : ") for i in range(0, len(my_list_2)): print(my_list_2[i])
Output
Elements of original array: 1 2 3 4 5 Elements of new array: 1 2 3 4 5
Explanation
A list is defined.
Another list is defined with ‘None’ elements.
The list is iterated over, and the elements from the first list are assigned to the second list.
This is the copied list.
This is displayed on the console.