Is there a better way to iterate to my dictionary data without using 3 nested for loops like what I am currently doing given this data below? Btw, i am using python 2.6.
data = {'08132012':
{
'id01': [{'code': '02343','status': 'P'},{'code': '03343','status': 'F'}],
'id02': [{'code': '18141','status': 'F'},{'code': '07777','status': 'F'}]
}
}
Here is the 3 for loops current code:
for date in data:
for id in data[date]:
for trans in data[date][id]:
print "Date: %s" % date
print "Processing id: %s" % id
print trans['code']
print trans['status']
//query to database
EDITED: valid data values
Given the nested nature of the data, I don’t think there’s any way avoid some nested loops somewhere.
However, you can avoid having to nest most of your program logic by writing a flattening generator for your data, like so: