I got an assignment where I have a tuple that I should convert to a list, replace the second element and revert back to a tuple. The problem I think I have is that the element is an int and should be replaced with a string. From what I can find here on the interweb I can't really work out a way to do this. Could you perhaps give me some pointers?
tupX = ("moose", 12, 1.98, "table", 7)
listX = list(tupX)
listX = [e.replace(1, "elevator")for e in listX]
tupY = tuple(listX
print(tupY)
The error message I get is TypeError: replace() argument 1 must be str, not int
.
Any pointers on this? Regards
Dragonthoughts
2,2348 gold badges27 silver badges28 bronze badges
asked Oct 15, 2018 at 11:23
1 Answer 1
Just set new value by index:
tupX = ("moose", 12, 1.98, "table", 7)
listX = list(tupX)
listX[1] = 'elevator'
tupY = tuple(listX)
print(tupY)
answered Oct 15, 2018 at 11:25
lang-py