My Coding >
Programming language >
Python >
Python LIST HowTo
Python LIST HowTo (Page: 3)Go to Page: Shallow copying lists in PythonShallow list copying in Python can only be used when it is known, that the list has only one layer of stored data. Use method .copy()
Use full slice [:]
Use constructor list()
Use interpolation [*list]
Use function copy() from module copy
Multiplication by number list * 1This can make one copy of the list of with using other numbers, you can make repetition of lists
Fastes way of shallow copyingThe difference in copying performance can only be observed from small (len()<1000) elements. For bigger lists, shallow copies are approximately similar in their performance. Almost 10 times difference for very small lists (len()<10) and about 5 times difference for lists of medium size (len()<100) can actually be ignored in most of the cases. The final performance will be distributed like that, from fastest to slowest:
Deep copying lists in PythonTo make a full copy of the list content in Python, it is necessary to use deepcopy() function from module copy. Only this way can guarantee creating of the full independent copy of your lists. Usually, in the program, if you are not sure of the lists content, it is better to use deepcopy rather than shallow copy
Wrong copying lists in Python
|
Last 10 artitles
9 popular artitles
|
|
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. |