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', ['""'], 'mask', ['12'], 'type', ['ip_mask']]

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

[('', '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', ['""'], 'mask', ['20'],
'type', ['ip_mask']], ['ip', ['""'], 'mask', ['32'], 'type', ['ip_mask']

And the expected output:  

[('', '20'), ('', '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

46 out of 50 users completed this Bite.
Will you be the 47th person to crack this Bite?
Resolution time: ~71 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 5.67 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help