I’ve been working with Python’s ctypes module to shuttle data back and forth with a Windows .dll. One of the arrays the .dll fills is pre-allocated, but not necessarily all used. So I’ll wind up with a tuple like this:
(42, 33, 89, 0, 0, 0, 0, 0, 0)
It’s harmless to have the trailing 0’s, but for the sake of tidiness I want to remove them. Python tuples are immutable, so there aren’t any methods for just chopping them out. What you can do is fill a new tuple with just the non-0 values. Here’s a fancy way of doing this using list comprehensions.
>>>some_data_fixed = tuple([x for x in some_data if x])
(42, 33, 89)
Which basically says to fill a list with x for every element x in some_data if it evaluates as true (meaning non-0), and then cast it to a tuple.
This example is in the official documentation for list comprehensions.