I couldn't write an easy-to-understand title ...
li = [
{'name': 'col_a', 'length': 10},
{'name': 'col_b', 'length': 8},
{'name': 'col_c', 'length': 6},
{'name': 'col_d', 'length': 4},
{'name': 'col_e', 'length': 2}
]
I want the total length up to the third item!
When
sum([d['length'] for d in li[:3]])
You can get it with.
for idx in range(len(li)):
print(sum(d.get('length', 0) for d in li[:i]]))
Like.
If you need a starting element, you can adjust it within the range.
Everyone loves it (?) For supporting fixed-length files, etc ...
Recommended Posts