Example:
a = (1,2,[3,4,5],6)
Can we make changes and make tuple a
to give output as
>> (1,2,[3,0,5],6)?
Example:
a = (1,2,[3,4,5],6)
Can we make changes and make tuple a
to give output as
>> (1,2,[3,0,5],6)?
Yes like this:
a=(1,2,[3,4,5],6)
a[2][1] = 0
print(a)
What this does is gets the third element of the tuple wich is the list then gives the new data to the second element in the list then prints out the whole tuple with the new data in the list. I believe this is what you want to do please tell me if it isnt thanks.
Yes you can, like this:
a=(1,2,[3,4,5],6)
a[2][1]=0
print(a)
This takes the third element of a, which is the list and modifies each element in the list separately.
You might do it using pop
and insert
methods of that list
a = (1,2,[3,4,5],6)
a[2].pop(1)
a[2].insert(1, 0)
print(a)
Output:
(1, 2, [3, 0, 5], 6)
These methods acts in place, pop
removes element at index 1
(indices are starting from 0
), insert
inserts before element at index 1
value 0
.
You can do this:
a=(1,2,[3,4,5],6)
b = a[2]
b[1]=0
a
Output: (1, 2, [3, 0, 5], 6)
Python tuples are immutable. The only solution to change the content is to create a new tuple. Here note that your tuple element at index 2 stores an array, which is mutable by reference so you can mutate the array but not assigning a new array.
In other words:
t = (1, 2, [3, 4], 5)
# New tuple:
t = (1, 2, [3, 4, 5], 5) # or
t = (t[0], t[1], [3, 4, 5], t[3]) # to copy values
# Mutate the array:
t[2].append(5)
In your case:
a = (1, 2, [3, 4, 5], 6)
# Mutate the array
a[2][1] = 0