Issue:
If I need to flatten a list of lists I use something like this list comprehension to flatten into a single list:
[item for sublist in l for item in sublist]
I have a dictionary where some of the values are list of lists and I need to flatten these into single lists prior to importing into Pandas.
Current data:
defaultdict(list,{'object network fake-1': [' host 10.0.0.1'],'object network fake12': [' host 10.0.0.12'],'object network fake2': [' host 10.0.0.2 '],'object network fake3': [' host 10.0.0.0 255.255.255.0'],'object network fake4': [' host 10.0.0.4'],'object network fake5': [' host 10.0.0.5'],'object-group network prt-apps': [' network-object object fake-1',' network-object object fake2',' network-object object fake3',' network-object object fake121'],'object-group network prt-apps2': [' network-object object fake4',' group-object prt-apps',[' network-object object fake-1',' network-object object fake2',' network-object object fake3',' network-object object fake121']],'object-group network prt-apps3': [' network-object object fake5',' group-object prt-apps2',[' network-object object fake4',' group-object prt-apps',[' network-object object fake-1',' network-object object fake2',' network-object object fake3',' network-object object fake121']]]})
Desired data structure:
defaultdict(list,{'object network fake-1': [' host 10.0.0.1'],'object network fake12': [' host 10.0.0.12'],'object network fake2': [' host 10.0.0.2 '],'object network fake3': [' host 10.0.0.0 255.255.255.0'],'object network fake4': [' host 10.0.0.4'],'object network fake5': [' host 10.0.0.5'],'object-group network prt-apps': [' network-object object fake-1',' network-object object fake2',' network-object object fake3',' network-object object fake121'],'object-group network prt-apps2': [' network-object object fake4',' group-object prt-apps',' network-object object fake-1',' network-object object fake2',' network-object object fake3',' network-object object fake121'],'object-group network prt-apps3': [' network-object object fake5',' group-object prt-apps2',' network-object object fake4',' group-object prt-apps',' network-object object fake-1',' network-object object fake2',' network-object object fake3',' network-object object fake121']})
I have searched SO for this and do not see an example that I could use. Is there a simple way to flatten these kind of 'list of list' containers within a dictionary value?
This is the way I have processed other dictionary structures when consuming in Pandas but it does not work with the first dictionary above:
pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in asa.iteritems() ]))