TheDeveloperBlog.com

Home | Contact Us

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

Python Copy List

These Python examples copy lists with the slice syntax and the extend method. Benchmarks are provided.

Copy list. A list references a region of memory.

When assigned, the list reference itself is copied. We sometimes need instead to copy all the elements of a list into a new list. The new list then can be independently modified.

Based on:

Python 3

Example. We use the slice syntax to copy a list. With slices, we separate two numbers with a colon. But when those numbers are omitted, we consider the slice to equal the entire list. So an unspecified slice copies a list.

Tip: This is the shortest syntax form. It requires only a list reference and three characters.

Python program that copies list

# A list:
list1 = [10, 20, 30]

# Copy list1 with slice syntax.
list2 = list1[:]

# Modify list2.
list2[0] = 0

print(list1)
print(list2)

Output

[10, 20, 30]
[0, 20, 30]

Extend. Next, we use the extend method to copy a list. We first need to allocate a new, empty list. Then we extend that empty list (list2) with the original list. Extend receives a sequence type.

Note: This approach requires two lines. It is also less efficient. Please see the benchmark section.

Python program that uses extend, copies list

list1 = [10, 20, 30]

# Create empty list, then extend it.
list2 = []
list2.extend(list1)

# The lists are separate.
list2[0] = 0

print(list1)
print(list2)

Output

[10, 20, 30]
[0, 20, 30]

Performance. Here we consider performance of list copying. Version 1 tests the slice syntax. Version 2 instead uses the extend method (after creating an empty list). We repeat these operations ten million times.

List: The list contains five numbers. We use while-loops to repeatedly copy those five elements.

While, For

Result: Taking a slice to copy a list is faster. It required just 4.7 seconds, versus 5.3 for the extend method version.

Python program that times list copying

import time

list1 = [100, 200, 300, 400, 500]

print(time.time())

# Version 1: slice
i = 0
while i < 10000000:
    list2 = list1[:]
    i += 1

print(time.time())

# Version 2: extend
i = 0
while i < 10000000:
    list2 = []
    list2.extend(list1)
    i += 1

print(time.time())

Output

1395252155.968703
1395252160.704971   Slice  = 4.736 s
1395252166.045275   Extend = 5.340 s

Summary. A full-list slice is a good way to copy a list. It requires the least amount of syntax. And it performs faster than the alternative approach that uses the extend method. Slicing wins on both considerations.


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