Python provide multiple ways to achieve the desired result. For example, to copy a list to another list there are different methods:
#Method 1
lstcopy = lst[:]
print("Cloning By list slicing lst[:] ",lstcopy)
#Method2
lstcopy = lst.copy()
print("Cloning By list copying lst.copy() ",lstcopy)
#Method3
lstcopy = [i for i in lst]
print("Cloning By list Comprehension ",lstcopy)
#Method4
lstcopy = []
for i in lst :
lstcopy.append(i)
print("Cloning By list append ",lstcopy)
Thanks,
I hope you learn something new from this article! Thanks for your support
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3