- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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.
Advertisements