How to copy (clone) a list in Python

4 Answers

0 votes
programming = ['Python', 'C#', 'Java', 'C', 'PHP', "C++", "Java"]

new_list = programming[:]

print(new_list)


'''
run:

['Python', 'C#', 'Java', 'C', 'PHP', 'C++', 'Java']

'''

 



answered Oct 30, 2017 by avibootz
edited Oct 30, 2017 by avibootz
0 votes
programming = ['Python', 'C#', 'Java', 'C', 'PHP', "C++"]

new_list = list(programming)

print(new_list)


'''
run:

['Python', 'C#', 'Java', 'C', 'PHP', 'C++']

'''

 



answered Oct 30, 2017 by avibootz
edited Oct 30, 2017 by avibootz
0 votes
import copy

programming = ['Python', 'C#', 'Java', 'C', 'PHP', "C++", "C"]

new_list = copy.copy(programming)

print(new_list)


'''
run:

['Python', 'C#', 'Java', 'C', 'PHP', 'C++', 'C']

'''

 



answered Oct 30, 2017 by avibootz
0 votes
import copy

programming = ['Python', 'C#', 'Java', 'C', 'PHP', "C++", "Python"]

new_list = copy.deepcopy(programming)

print(new_list)


'''
run:

['Python', 'C#', 'Java', 'C', 'PHP', 'C++', 'Python']

'''

 



answered Oct 30, 2017 by avibootz

Related questions

1 answer 200 views
2 answers 100 views
1 answer 75 views
1 answer 139 views
1 answer 156 views
4 answers 257 views
257 views asked Apr 18, 2021 by avibootz
...