Python Delete Tuple | How to Delete Tuples with Examples

Python Delete Tuple

Tuples in Python are immutable, which means you cannot delete individual elements inside a tuple. However, you can delete an entire tuple variable using the del statement.

Python में tuples immutable होते हैं, इसलिए आप tuple के individual elements को delete नहीं कर सकते। लेकिन आप पूरी tuple variable को del स्टेटमेंट से delete कर सकते हैं।

Why and When to Delete a Tuple?

  • To free up memory when a tuple is no longer needed.
  • To remove a variable completely from the current namespace.
  • Since tuples are immutable, deleting elements inside is not possible, but deleting the whole tuple variable is allowed.
  • जब tuple की जरूरत खत्म हो जाए तो memory खाली करने के लिए।
  • किसी variable को पूरी तरह से current namespace से हटाने के लिए।
  • Tuples immutable होते हैं, इसलिए elements को delete नहीं किया जा सकता, लेकिन पूरा tuple variable delete किया जा सकता है।
Example 1: Delete Entire Tuple Variable
t = (1, 2, 3, 4)
del t
print(t)  # This will cause an error

Deletes the whole tuple variable 't'. Trying to print afterwards causes an error because 't' no longer exists.

't' नाम का पूरा tuple variable delete हो जाता है। बाद में print करने पर error आता है क्योंकि 't' मौजूद नहीं है।

Output / Error:

NameError: name 't' is not defined
Example 2: Attempt to Delete Single Element (Not Allowed)
t = (1, 2, 3, 4)
del t[0]  # This will cause an error

Trying to delete an individual element from a tuple causes a TypeError because tuples are immutable.

Tuple के किसी एक element को delete करने पर TypeError आता है क्योंकि tuples immutable होते हैं।

Output / Error:

TypeError: 'tuple' object doesn't support item deletion
Example 3: Reassign Tuple Instead of Deleting Elements
t = (1, 2, 3, 4)
t = t[1:]  # Creates a new tuple without first element
print(t)

Since you can't delete elements, you can create a new tuple excluding elements you want to remove by slicing and reassigning.

Elements delete न कर पाने की वजह से, आप slicing से नया tuple बना सकते हैं जिसमें unwanted elements excluded हों।

Output / Error:

(2, 3, 4)