-1

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
MinneapolisCoder9
  • 601
  • 1
  • 11
  • 29
  • 1
    What is your desired output? – Nick Jun 27 '20 at 23:35
  • 2
    `value[key + 1]` is just wrong - `value` here is a string, int, or None, none of which is `key` a meaningful index for. Closer would be`texts[key + 1]`, but that will fail for the highest key, so use `texts.get(key + 1)` instead (which will return `None` for the highest key). – jasonharper Jun 27 '20 at 23:50
  • 1
    You have basic typos in your code, such as `texts.items()` with the example dict being `sample_dict` If you are trying to test the next item in `sample_dict` you would test `if value is not None and sample_dict[key + 1] is not None` – dawg Jun 27 '20 at 23:51
  • 3
    Why are you using a dictionary with numeric keys and order that matters? Use a list. – Barmar Jun 27 '20 at 23:55
  • 1
    as others have responded in various ways: "I'm not sure if this is appropriate": short answer is no. – user120242 Jun 28 '20 at 00:11
  • 1
    as to a solution to your problem, just use sample_dict.get(key+1) instead of [key + 1] – user120242 Jun 28 '20 at 00:26
  • @dawg thank you for pointing out that I messed up my variable names before posting. Edited per your comments. – MinneapolisCoder9 Jun 28 '20 at 14:15
  • @user120242 - thank you for opening my eyes to dictionary.get(), which is elaborated on [here](https://stackoverflow.com/a/11041421/5825523). – MinneapolisCoder9 Jun 28 '20 at 14:28

2 Answers2

1

If there is a chance that your last key is not None, you should also test for it.

for key, value in sample_dict.items():
    next_key = key + 1
    if (next_key <= len(sample_dict)):
        if value is not None and sample_dict[next_key] is not None:  # Error
            print(key, value)
            desired_dict[key] = value
NWiogrhkt
  • 113
  • 1
  • 9
1

The only order that a dictionary maintains is the order in which keys were added to the dictionary, so the only meaning 'after' can have on a dictionary is the 'key' added afte rthis one.

Since the ordering is important, and all of your keys are integers, ou should use lists - and there is a definitie and clear 'after', and that makes sense even when you insert a value.

This is what I would do :

sample_data= [None, 'red', 42,None,None,
           'lava', None, None, 'cats',
           'pony', None]


desired = {}
for index, value in enumerate(sample_data):
    if index < len(sample_data) and \
           value is not None and \
           sample_data[index + 1] is not None: 
         print(key, value)
         desired_dict[index] = value
Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33