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

Dragonthoughts
2,2348 gold badges27 silver badges28 bronze badges
asked Oct 15, 2018 at 11:23

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)
answered Oct 15, 2018 at 11:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.