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.
Append an item to tuple tuple is immutable, but you can concatenate multiple tuples with the + operator. At this time, the original object remains unchanged, and a new object is generated. Only tuples can be concatenated. It cannot be concatenated with other types such as list .
You can also add tuples to a set. And like normal elements, you can add the same tuple only once.
You can combine tuples to form a new tuple. The addition operation simply performs a concatenation with tuples. You can only add or combine same data types.
The primary way in which tuples are different from lists is that they cannot be modified. This means that items cannot be added to or removed from tuples, and items cannot be replaced within tuples. You can, however, concatenate 2 or more tuples to form a new tuple. This is because tuples cannot be modified.
the trick is to send it inside brackets so it doesn't get exploded
data.update([('A', 20160000, 22)])
just use data.add
. Here's an example:
x = {(1, '1a'), (2, '2a'), (3, '3a')}
x.add((4, '4a'))
print(x)
Output: {(3, '3a'), (1, '1a'), (2, '2a'), (4, '4a')}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With