I have a tuple:
my_tup=(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9)
I would like to modify a value in this tuple. Because tuples are immutable, the obvious route doesn't work:
my_tup[:a]=50 #Raises an error, as expected
So to perform the modification, I'd like to copy the tuple and its elements while changing the target element.
My current solution is as follows:
my_tup=(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9)
args = keys(my_tup)
NamedTuple{args}(i!=:a ? getfield(my_tup, i) : 50 for i in args)
But this seems verbose or as though there should already be a function for it in the standard library.
Is there a better way?
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
Copy a tuple. You cannot copy a list with the = sign because lists are mutables. The = sign creates a reference not a copy. Tuples are immutable therefore a = sign does not create a reference but a copy as expected.
You can use * operator to replicate a tuple a specified number of times. Slicing refers to extracting a subpart of the mentioned sequence. Tuple- slices are same as the List- slices. Tuple slice is a tuple itself.
A tuple is immutable--it will always contain the same objects. If those objects are mutable, they might themselves be mutated, but the tuple will still contain that object.
How to modify a tuple in Python Author: Aditya Raj Last Updated: August 27, 2021 We know that tuple is an immutable data type unlike a python dictionary or a list. That means, we cannot modify a tuple by any means. But, We may need to modify a tuple. In this case, we have no other option to create a new tuple with the desired elements.
The first tuple will contain elements from index 0 to i-1 of the original tuple. The second tuple will contain elements from index “i” to last. After that, we will create a tuple with a single element which has to be inserted into the tuple at index i.
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8) Updated tuple after deleting element at index 3 is: (1, 2, 3, 5, 6, 7, 8) How to modify an element at a specific index in a tuple To modify an element at index “i” of a tuple, we will create two slices of the original tuple. The first slice will contain elements from index 0 to i-1 of the original tuple.
To modify an element at index “i” of a tuple, we will create two slices of the original tuple. The first slice will contain elements from index 0 to i-1 of the original tuple. The second slice will contain elements from index “i+1” to last. After that, we will update the element at index “i” by inserting the new value at the position as follows.
You can merge
two named tuples:
julia> xs = (a = 1, b = 2, c = 3)
(a = 1, b = 2, c = 3)
julia> ys = merge(xs, (; a = 50))
(a = 50, b = 2, c = 3)
David Varela has the correct answer, "use merge
". It is an important technique.
on creating plain Tuples (unnamed Tuples) and NamedTuples
Both Tuples and NamedTuples are constructed using parentheses:
julia> a_tuple = (1, 2)
(1, 2)
julia> a_namedtuple = (a = 1, b = 2)
(a = 1, b = 2)
If we try construct a Tuple or a NamedTuple with exactly one entry that same way, it does not work; instead the values are assigned named variables directly.
julia> not_a_tuple = ("xyz");
julia> not_a_tuple, typeof(not_a_tuple)
("xyz", String)
julia> not_a_namedtuple = (abc = "xyz");
julia> not_a_namedtuple, typeof(not_a_namedtuple)
("xyz", String)
To construct Tuples and NamedTuples that have a single value, we let Julia know that we are working with [Named]Tuples. An easy way to do this, and one that works for both kinds of Tuples, is to add a comma ',' before the closing parenthesis. For visual emphasis, I separate the commas from the values; it is not necessary to do that in your source code.
julia> a_tuple = (0.5 ,)
(0.5,)
julia> a_namedtuple = (onehalf = 0.5 ,)
(onehalf = 0.5,)
on substituting values in NamedTuples
julia> namedtuple = (a = 1, b = 2, c = 3); # the initial NamedTuple
julia> changes_to_make = (b = 0 ,); # the modifications intended
julia> changed_namedtuple = merge(namedtuple, changes_to_make)
(a = 1, b = 0, c = 3)
julia> namedtuple = (a = 1, b = 2, c = 3); # the initial NamedTuple
julia> changes_to_make = (b = 0, c = 7); # the modifications intended
julia> changed_namedtuple = merge(namedtuple, changes_to_make)
(a = 1, b = 0, c = 7)
julia> my_tup=(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9)
(a = 1, b = 2, c = 3,d = 4, e = 5, f = 6, g = 7, h = 8, i = 9)
julia> new_one = (my_tup..., a=50)
(a = 50, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9)
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