0

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

1 Answer 1

2

Just set new value by index:

tupX = ("moose", 12, 1.98, "table", 7)
listX = list(tupX)
listX[1] = 'elevator'
tupY = tuple(listX)
print(tupY)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.