"Total flattening" of a tuple

Giuseppe Bilotta oblomov at freemail.it
Wed Jan 17 17:07:50 EST 2001


Hello,

I'd like to know if it's possible to "totally flatten" a tuple, or to decompose it ... ok, an example will (hopefully) explain what
I want.

Let's say that I have to pack (struct.pack) a tuple of numbers (actually, originally a list). I would go like this:

result = ""
for i in mylist :
    result = result + struct.pack('B', i)

Now, I wondered if it was possible to do something like:

packmode = str(len(mylist)) + 'B' # this prepares the packing structure
result = struct.pack(packmode, flatten(mylist))


where "flatten" would be a command to unpack the sequence mylist, *without* the external ( ... ) [that is, tuple(mylist) doesn't
work, because it would make the command in

struct.pack(packmode, (tuple))

while the "pack" command wants

struct.pack(packmode, tuple_element_1, tuple_element_2, ...)

(that is, without the external (...) in the tuple).

Is this feasable? If yes, how?


Thank you for your attention,

--
Giuseppe Bilotta







More information about the Python-list mailing list