Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:
('Product', '500.00', '1200.00')
Possible?
Thanks!
You can directly enter duplicate items in a Python tuple as it doesn't behave like a set(which takes only unique items).
When it is required to repeat a tuple 'N' times, the '*' operator can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error.
Tuple — A tuple is an immutable ordered collection that allows duplicate elements.
copy. copy() and copy. deepcopy() just copy the reference for an immutable object like a tuple.
You can cast it to a list, insert the item, then cast it back to a tuple.
a = ('Product', '500.00', '1200.00') a = list(a) a.insert(3, 'foobar') a = tuple(a) print a >> ('Product', '500.00', '1200.00', 'foobar')
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