Python program to copy all elements of one array into another array



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

 Live Demo

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.

Updated on: 2021-04-15T13:48:39+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements