Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone or copy a set in Python?

For copying a list: shallow_copy_of_list = old_list[:].

For copying a dict: shallow_copy_of_dict = dict(old_dict).

But for a set, I was worried that a similar thing wouldn't work, because saying new_set = set(old_set) would give a set of a set?

But it does work. So I'm posting the question and answer here for reference. In case anyone else has the same confusion.

like image 443
Evgeni Sergeev Avatar asked Apr 21 '14 16:04

Evgeni Sergeev


People also ask

Can we copy a set in Python?

set copy() in pythonThe copy() method returns a shallow copy of the set in python. If we use “=” to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set.

What does clone () do in Python?

clone() function makes an exact copy of the original image. One can use this clone image to manipulate without affecting the original image. clone() is one of the most important function because it helps in safe manipulation of image.

What does Copy () do in Python list?

Python copy() method copies the list and returns the copied list. It does not take any parameter and returns a shallow copy of the list. A shallow copy is the one that does not show any modification on the original list.

What is the use of the copy () method?

Definition and Usage The copy() method returns a copy of the specified list.


2 Answers

Both of these will give a duplicate of a set:

shallow_copy_of_set = set(old_set) 

Or:

shallow_copy_of_set = old_set.copy() #Which is more readable. 

The reason that the first way above doesn't give a set of a set, is that the proper syntax for that would be set([old_set]). Which wouldn't work, because sets can't be elements in other sets, because they are unhashable by virtue of being mutable. However, this isn't true for frozensets, so e.g. frozenset(frozenset(frozenset([1,2,3]))) == frozenset([1, 2, 3]).

So a rule of thumb for replicating any of instance of the basic data structures in Python (lists, dict, set, frozenset, string):

a2 = list(a)      #a is a list b2 = set(b)       #b is a set c2 = dict(c)      #c is a dict d2 = frozenset(d) #d is a frozenset e2 = str(e)       #e is a string #All of the above give a (shallow) copy. 

So, if x is either of those types, then

shallow_copy_of_x = type(x)(x) #Highly unreadable! But economical. 

Note that only dict, set and frozenset have the built-in copy() method. It would probably be a good idea that lists and strings had a copy() method too, for uniformity and readability. But they don't, at least in Python 2.7.3 which I'm testing with.

like image 118
Evgeni Sergeev Avatar answered Sep 26 '22 22:09

Evgeni Sergeev


Besides the type(x)(x) hack, you can import copy module to make either shallow copy or deep copy:

In [29]: d={1: [2,3]}  In [30]: sd=copy.copy(d)     ...: sd[1][0]=321     ...: print d {1: [321, 3]}  In [31]: dd=copy.deepcopy(d)     ...: dd[1][0]=987     ...: print dd, d {1: [987, 3]} {1: [321, 3]} 

From the docstring:

Definition: copy.copy(x) Docstring: Shallow copy operation on arbitrary Python objects. 
like image 38
zhangxaochen Avatar answered Sep 23 '22 22:09

zhangxaochen