Use a tuple. my_list = ([1], [1, 2], [1, 2, 3])
my_list now immutable, and anytime you want to use a mutable copy, you can just use list(my_list)
>>> my_list = ([1], [1, 2], [1, 2, 3]) >>> def mutate(aList): aList.pop() return aList >>> mutate(list(my_list)) [[1], [1, 2]] >>> my_list ([1], [1, 2], [1, 2, 3]) >>>
As someone caught my attention, this solution is not reliable. The tuple itself is not changed, but its elements (if they are mutable objects - which lists).
source share