Problem
How do you iterate through all lines in a dictionary while checking 1 line before or after?
When iterating through the items, I error out in my current approach because I refer to [key +/- 1]. I usually find hack solutions, but wonder if you may have a better technique.
Finally, dicts are unordered, so I'm not sure if this is appropriate.
Attempt
sample_dict = {0: None,
1: 'red',
2: 42,
3: None,
4: None,
5: 'lava',
6: None,
7: None,
8: 'cats',
9: 'pony',
10: None}
desired_dict = {}
for key, value in sample_dict.items():
if value is not None and sample_dict[key + 1] is not None:
print(key, value)
desired_dict[key] = value