Expert Mod 2.5K+
P: 2,851
|
I have a tuple
tup = (1,2,3,())
I want to put a value 4 in the last position of tup
i.e. i want tup to be (1,2,3,4)
how can i do that
Extremist is correct - tuples are immutable. You could do this however: - >>> tup = (1,2,3,())
-
>>> tup
-
(1, 2, 3, ())
-
>>> tup = (tup[0], tup[1], tup[2], 4)
-
>>> tup
-
(1, 2, 3, 4)
-
>>>
-
>>>
-
>>> lst = []
-
>>> for i in tup:
-
... lst.append(i)
-
...
-
>>> lst.append(5)
-
>>> lst
-
[1, 2, 3, 4, 5]
-
>>> tup = tuple(lst)
-
>>> tup
-
(1, 2, 3, 4, 5)
-
>>>
| |