DhiwaTdG DhiwaTdG - 3 months ago 12
Python Question

Python: Append tuple to a set with tuples

Following is my code which is a set of tuples:

data = {('A',20160129,36.44),('A',20160201,37.37),('A',20160104,41.06)};
print(data);


Output:
set([('A', 20160129, 36.44), ('A', 20160104, 41.06), ('A', 20160201, 37.37)])


How do I append another tuple
('A', 20160000, 22)
to
data
?

Expected output:
set([('A', 20160129, 36.44), ('A', 20160104, 41.06), ('A', 20160201, 37.37), ('A', 20160000, 22)])


Note: I found a lot of resources to append data to a set but unfortunately none of them have the input data in the above format. I tried
append
,
|
&
set
functions as well.

Answer

the trick is to send it inside brackets so it doesn't get exploded

data.update([('A', 20160000, 22)])
Comments