Pybites Logo

Nested list extraction

Level: Intermediate (score: 3)

Sometimes dealing with data can be a real struggle.

Sometimes it's delivered in a undesirable format that you have no control over. Sometimes things can get ugly.  

But you're a PyBites Ninja! You persevere, you overcome any obstacle!  

The networking team is writing a Python script to help keep track of inventory and they need some assistance. 

---      

They give you a list of data that looks something like this: 

data = [['ip', ['"172.16.0.0"'], 'mask', ['12'], 'type', ['ip_mask']]

And need to extract the IP address information into a list of 2-tuples(ip, mask) like this:  

[('172.16.0.0', '12'), ...]

---    

Here's another example of a deeply nested list:  

[['TEST', ['parent', [], 'uuid', ['"khk-yyas4h-323223-wewe-343er-3434-www"'], 
'display_name', ['"services"'], 'IPV4', [[['ip', ['"1.1.1.0"'], 'mask', ['20'],
'type', ['ip_mask']], ['ip', ['"2.2.2.2"'], 'mask', ['32'], 'type', ['ip_mask']
]]]]]]

And the expected output:  

[('1.1.1.0', '20'), ('2.2.2.2', '32')]

---

The IP information is nested inside lists-of-lists-of-lists-of...  It's really really badly structured data that has no business being in this format.

Can you help get requested information out of "this heap of trash"?