Sobes.tech
Junior — Middle

Is it possible to delete elements from a list if the original data is represented as a tuple?

sobes.tech AI

Answer from AI

In Python, tuples (tuple) are immutable, so you cannot directly delete an element from a tuple. However, you can create a new tuple excluding the desired elements.

For example, to remove an element with a specific value:

original_tuple = (1, 2, 3, 4, 5)
value_to_remove = 3
new_tuple = tuple(x for x in original_tuple if x != value_to_remove)
print(new_tuple)  # (1, 2, 4, 5)

If you need to modify the collection, it's better to use a list (list), which supports direct removal of elements.