"Total flattening" of a tuple

Alex Martelli aleaxit at yahoo.com
Wed Jan 17 17:55:15 EST 2001


"Giuseppe Bilotta" <oblomov at freemail.it> wrote in message
news:WIo96.76570$eB2.6161239 at news.infostrada.it...
    [snip]
> 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).

struct.pack(packmode, *mylist) works in Python 2.0; for older releases,
apply(struct.pack, tuple(mylist)) [I think you can omit the tuple() call,
in some of those older releases, but don't recall exactly -- been a while].


Alex






More information about the Python-list mailing list