python struct.pack(): pack multiple datas in a list or a tuple

Say i have a list or a tuple containing numbers of type long long,

x = [12974658, 638364, 53637, 63738363]

If want to struct.pack them individually, i have to use

struct.pack('<Q', 12974658)

or if i want to do it as multiple, then i have to explicitly mention it like this

struct.pack('<4Q', 12974658, 638364, 53637, 63738363)

But, how can i insert items in a list or tuple inside a struct.pack statement. I tried using for loop like this.

struct.pack('<4Q', ','.join(i for i in x))

got error saying expected string, int found, so i converted the list containing type int into str, now it gets much more complicated to pack them. Because the whole list gets converted into a string( like a single sentence).

As of now im doing some thing like

binary_data = '' x = [12974658, 638364, 53637, 63738363] for i in x: binary_data += struct.pack('<Q', i)

And i unpack them like

struct.unpack('<4Q', binary_data)

<strong>My question:</strong> is there a better way around, like can i directly point a list or tuple inside the struct.pack statement, or probably a one liner ?


You can splat, I'm sorry "unpack the argument list":

>>> struct.pack("<4Q", *[1,2,3,4]) '\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00'

If the length of the list is dynamic, you can of course build the format string at runtime too:

>>> x = [1, 2] # This could be any list of integers, of course. >>> struct.pack("<%uQ" % len(x), *x) '\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'


  • AngularJS - ngBind and Bootstrap Switch
  • Is a .txt file created in VB different than one I'd randomly create?
  • Xamarin PCLCrypto SHA256 give different hash
  • .NET video play library which allows to change the playback rate?
  • How to 'create temp table as select' in Slick?
  • Default parameter as generic type
  • MySQL Order by column = x, column asc?
  • Parse a date string in a specific locale (not timezone!)
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Django: Count of Group Elements
  • Using $this when not in object context
  • Array.prototype.includes - not transformed with babel
  • PHP - How to update data to MySQL when click a radio button
  • Volley JsonObjectRequest send headers in GET Request
  • Accessing IRQ description array within a module and displaying action names
  • How to check if every primary key value is being referenced as foreign key in another table
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Build own AppleScript numerical error handling
  • Weird JavaScript statement, what does it mean?
  • Websockets service method fails during R startup
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How to get next/previous record number?
  • Google cloud sdk not working when python points python3
  • 0x202A in filename: Why?
  • php design question - will a Helper help here?
  • Is there a mandatory requirement to switch app.yaml?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Sorting a 2D array using the second column C++
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?