What is a better way to deal with unknown list structures in python?
<p>My python program receives MIDI data from a C library.  Sometimes the data will look like this:</p>

<pre><code>[[[[240,0,1,116]]],[[[3,100,8,1]]],[[[107,247,0,0]]]]
</code></pre>

<p>and sometimes it will include timestamps like this:</p>

<pre><code>[[[[240,0,1,116],26738]],[[[3,100,8,1],26738]],[[[107,247,0,0],26738]]]
</code></pre>

<p>I need the data in an array of bytes, with the timestamp values discarded.  The code I wrote to do this is:</p>

<pre><code>        def convertMidiSysex(data):    
            while isinstance(data[0][0], list):
                out = []
                for index, value in enumerate(data):
                    out = out+value
                data = out

            out = array.array('B')
            for i in range(len(data)):
                if isinstance(data[i], list):
                    for j in range(len(data[i])):
                        out.append(data[i][j])
                        if out[-1] == 247:  # 0xF7 is marker to end sysex message
                            return out
</code></pre>

<p>I can't help feeling that I'm doing this the hard way.  Is there a better approach to this?</p>
