avatar Bite 285. Nested List Extraction

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"?

Login and get coding
go back Intermediate level
Bitecoin 3X

14 out of 17 users completed this Bite.
Will you be Pythonista #15 to crack this Bite?
Resolution time: ~94 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 6.5 on a 1-10 difficulty scale.
» Up for a challenge? 💪

We use Python 3.8