TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python List copy() method with Examples

Python List copy() method with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python List copy() Method

Python copy() method copies the list and returns the copied list. It does not take any parameter and returns a list. The method signature and examples are given below.

Signature

copy()

Parameters

No parameter

Return

It returns a copy of the list.

Let's see some examples of copy() method to understand it's functionality.

Python List copy() Method Example 1

A Simple example which copy a list ot another and makes an new list. See the example below.

# Python list copy() Method
# Creating a list
evenlist = [6,8,2,4] # int list
copylist = []
# Calling Method
copylist = evenlist.copy()
# Displaying result
print("Original list:",evenlist)
print("Copy list:",copylist)

Output:

Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]

Python List copy() Method Example 2

Here, we are using list slicing concept to create a copy of the list. See the example below.

# Python list copy() Method
# Creating a list
evenlist = [6,8,2,4] # int list
copylist = []
# Calling Method
copylist = evenlist[:]  # Copy all the elements
# Displaying result
print("Original list:",evenlist)
print("Copy list:",copylist)

Output:

Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]

Python List copy() Method Example 3

The assignment operator can also be used to copy a list into another. This is most general approach but not recommended.

# Python list copy() Method
# Creating a list
evenlist = [6,8,2,4] # int list
copylist = []
# Calling Method
copylist = evenlist  # Copy all the elements 
# Displaying result
print("Original list:",evenlist)
print("Copy list:",copylist)

Output:

Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]

Next TopicPython Lists




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf