-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathcopylist.py
More file actions
42 lines (25 loc) · 730 Bytes
/
copylist.py
File metadata and controls
42 lines (25 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /usr/bin/env python3
import sys
"""a fast way to make a shallow copy of a list"""
a = [1, 2, 3, 4, 5]
print(a[:])
"""adding an empty list yields a copy of initial list"""
a = [1, 2, 3, 4, 5]
print(a + []) # in python3, for small lists performs faster than a[:]
"""copy list by typecasting method"""
a = [1, 2, 3, 4, 5]
print(list(a))
"""using the list.copy() method (python3 only)"""
if sys.version_info.major == 3:
a = [1, 2, 3, 4, 5]
print(a.copy())
else:
# in python2 there exists the `copy` builtin module
from copy import copy
a = [1, 2, 3, 4, 5]
print(copy(a))
"""copy nested lists using copy.deepcopy"""
from copy import deepcopy
l = [[1, 2], [3, 4]]
l2 = deepcopy(l)
print(l2)