0

Please find the below code, once I execute this code I am getting 'tuple' object is not callable error please give the solution, thanks in advance.


`mytuple = ("apple", "banana", "cherry")
names = list (mytuple)
names.append("orange")
mytuple = tuple (names)

print(mytuple)`

Here I tried to add item("orange") to a tuple, we can't directly add or change items in the tuple because tuples are immutable, so that what I did is I converted the tuple to a list with new name called names then I was added with "orange" using append function and then I was changed that list(names) to tuple and print the mytuple.

then finally I am getting error.

1 Answer 1

0

Make sure that you have not inadvertently overwritten the built-in tuple function. Here is the corrected version of your code:

mytuple = ("apple", "banana", "cherry")



# Convert tuple to list
names = list(mytuple)

# Append "orange" to the list
names.append("orange")

# Convert list back to tuple
mytuple = tuple(names)

# Print the updated tuple
print(mytuple)

When executed, this will correctly output:

('apple', 'banana', 'cherry', 'orange')
1
  • It's great for getting the solution immediately.
    – Lakshmi
    Commented Jun 25 at 10:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.