· python

Python: (Conceptually) removing an item from a tuple

As part of some code I’ve been playing around I wanted to remove an item from a tuple which wasn’t particularly easy because Python’s tuple data structure is immutable.

I therefore needed to create a new tuple excluding the value which I wanted to remove.

I ended up writing the following function to do this but I imagine there might be an easier way because it’s quite verbose:

def tuple_without(original_tuple, element_to_remove):
    new_tuple = []
    for s in list(original_tuple):
        if not s == element_to_remove:
            new_tuple.append(s)
    return tuple(new_tuple)

Which can be used like so:

>>> tuple_without((1,2,3,4), 1)
(2, 3, 4)
>>> tuple_without((1,2,3,4), 0)
(1, 2, 3, 4)

The easiest approach seemed to be to build up a list containing all the values and then convert it to a tuple by using the http://docs.python.org/2/library/functions.html#tuple function.

It’d be cool if there was a way to transform a tuple like this but I couldn’t find such a function in my travels.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket