Package: nestedfetch
ℕ𝕖𝕤𝕥𝕖𝕕𝔽𝕖𝕥𝕔𝕙OutlineOverviewInstallationUsageExamplesFetch ValueSet ValueFlatten Nested ListsHow to ContributeOverviewNestedFetchprovides syntactic sugar 🍬 to deal with a nested pythondictionaryor a nestedlist🐍You canget,set,updateandflattenvalues from a deeply nested dictionary or a list with a more concise, easier and aKeyError,IndexErrorfree way 😌data={"league":"Champions League","matches":[{"match_id":"match_1","goals":[{"time":13,"scorrer":"Lionel Messi","assist":"Luis Suarez"},{"time":78,"scorrer":"Luis Suarez","assist":"Ivan Rakitic"}]},{"match_id":"match_2","goals":[{"time":36,"scorrer":"C. Ronaldo","assist":"Luka Modric"}]}]}InstallationNestedFetchworks with Python3.You can directly install it viapip$pip3installnestedfetchUsageImport the methods from the package.fromnestedfetchimportnested_get,nested_setNo need to instantiate any object, just use the methods specifying valid parameters.ExamplesFetch Datanested_get(data,keys,default=None,flatten=False)@Argumentsdata:dict/listkeys=>Listofsequentialkeysleadingtothedesiredvaluetofetchdefault=>Specifiesthedefaultvaluetobereturnedifanyspecifiedkeyisnotpresent.Ifnotspecified,itwillbeNoneflatten=>Specifieswhethertoflattenthereturnedvalue@ReturnReturnsthefetchedvalueifitexists,orreturnsspecifieddefaultvalueFetchsimple nested data :data={'name':'Jesse Pinkman','details':{'address':{'city':'Albuquerque'}}}res=nested_get(data,['details','address','city'])# res = AlbuquerqueFetchsimple nested data withdefaultvalue:data={'name':'Jesse Pinkman','details':{'address':{'city':'Albuquerque'}}}res=nested_get(data,['details','address','state'],default=-1)# res = -1Fetchnested data:data={'name':'Jesse Pinkman','details':{'address':[{'city':'Albuquerque'},{'city':'El Paso'}]}}res=nested_get(data,['details','address','city'])# res = ['Albuquerque','El Paso']Fetchnested data withdefaultvalue:data={'name':'Jesse Pinkman','details':{'address':[{'city':'Albuquerque'},{'city':'El Paso'},{'state':'New Mexico'}]}}res=nested_get(data,['details','address','city'],default=None)# res = ['Albuquerque','El Paso', None]Fetchnested data by specifingindex:data={'name':'Walter White','details':{'address':[{'city':'Albuquerque'},{'city':'El Paso'}]}}res=nested_get(data,['details','address','city',0])# res = AlbuquerqueFetchnested data withoutflatten:data={"league":"Champions League","matches":[{"match_id":"match_1","goals":[{"time":13,"scorrer":"Lionel Messi","assist":"Luis Suarez"},{"time":78,"scorrer":"Luis Suarez","assist":"Ivan Rakitic"}]},{"match_id":"match_2","goals":[{"time":36,"scorrer":"C. Ronaldo","assist":"Luka Modric"}]}]}res=nested_get(data,['matches','goals','scorrer'])# res = [['Lionel Messi', 'Luis Suarez'], ['C. Ronaldo']]Fetchnested data withflatten:data={"league":"Champions League","matches":[{"match_id":"match_1","goals":[{"time":13,"scorrer":"Lionel Messi","assist":"Luis Suarez"},{"time":78,"scorrer":"Luis Suarez","assist":"Ivan Rakitic"}]},{"match_id":"match_2","goals":[{"time":36,"scorrer":"C. Ronaldo","assist":"Luka Modric"}]}]}res=nested_get(data,['matches','goals','scorrer'],flatten=True)# res = ['Lionel Messi', 'Luis Suarez', 'C. Ronaldo']Set / Update  Datanested_set(data,keys,value,create_missing=False):@Argumentsdata=>dict/listkeys=>Listofsequentialkeysleadingtothedesiredvaluetoset/updatevalue=>Specifiesthevaluetoset/updatecreate_missing=>Specifieswhethertocreatenewkeywhilebuildingupifthespecifiedkeydoesnotexists@ReturnReturnsthenumberofvaluesupdatedUpdatevalue of simple nested data :data={'name':'Jesse Pinkman','details':{'address':{'city':'Albuquerque'}}}res=nested_set(data,['details','address','city'],"Denver")# res = 1# data = {#             'name': 'Jesse Pinkman',#             'details': {#                 'address':{#                     'city': 'Denver'#                 }#             }#         }Updatenested data:data={'name':'Jesse Pinkman','details':{'address':[{'city':'Albuquerque'},{'city':'El Paso'}]}}res=nested_set(data,['details','address','city'],"Denver")# res = 2# data = {#     'name': 'Jesse Pinkman',#     'details': {#         'address':[{#             'city': 'Denver'#         },{#             'city': 'Denver'#         }]#     }# }Updatenested data withindex:data={'name':'Jesse Pinkman','details':{'address':[{'city':'Albuquerque'},{'city':'El Paso'}]}}res=nested_set(data,['details','address',0,'city'],"Denver")# res = 1# data = {#     'name': 'Jesse Pinkman',#     'details': {#         'address':[{#             'city': 'Denver'#         },{#             'city': 'El Paso'#         }]#     }# }Setnested data withcreate_missing:data={'name':'Jesse Pinkman','details':{'address':{'city':'Albuquerque'}}}res=nested_set(data,['details','address','state'],"New Mexico",create_missing=True)# res = 1# data = {#             'name': 'Jesse Pinkman',#             'details': {#                 'address':{#                     'city': 'Denver',#                     'state': 'New Mexico'#                 }#             }#         }Flatten Nested Listsflatten_data(data):@Argumentsdata=>listoflist@ReturnReturnstheflattenedlistFlattenList of Listsdata=[[['This','is'],['flattened','data']]]res=flatten_data(data)# res = ['This','is','flattened','data']How to contributeContributions are welcome 😇.Feel free to submit a patch, report a bug 🐛 or ask for a feature 🐣.Please open an issue first to encourage and keep track of potential discussions 📝.