-2

I have an dictionary as follows:

defaultdict(None, {
    'Category 1': [
        {'id': 24, 'name': 'Video 1', 'cat_id': 16, 'tut_id': 14, 'description': ''}, 
        {'id': 28, 'name': 'Video 5', 'cat_id': 16, 'tut_id': 14, 'description': ''}
    ], 
    'Category 3': [
        {'id': 25, 'name': 'Video 3', 'cat_id': 18, 'tut_id': 14, 'description': ''}, 
        {'id': 29, 'name': 'Video 6', 'cat_id': 18, 'tut_id': 14, 'description': ''}
    ], 
    'Category 2': [
        {'id': 26, 'name': 'Video 2', 'cat_id': 17, 'tut_id': 14, 'description': ''}, 
        {'id': 27, 'name': 'Video 4', 'cat_id': 17, 'tut_id': 14, 'description': ''}
    ]
})

And in my view I have next code:

video_id = int(request.POST.get('id', None))
cat_id = int(request.POST.get('cat_id', None))
cat_name = int(request.POST.get('cat_name', None))
tut_id = int(request.POST.get('tut_id', None))

I want to get next and previous item from the dictionary. If there are items onder same category get them, and if there are not, get the item from the next or previous category.

I have no idea how to start

Expected output

video_id = 3
cat_id = 18
cat_name = "Category 3"
tut_id = 14

next = {'id': 29, 'name': 'Video 6', 'cat_id': 18, 'tut_id': 14, 'description': ''}
prev = {'id': 28, 'name': 'Video 5', 'cat_id': 16, 'tut_id': 14, 'description': ''}

Any idea how to do that?

Boky
  • 11,554
  • 28
  • 93
  • 163

1 Answers1

0

The defaultdict can be flattened to a list of dicts like this. Assuming, you're using the latest version of python, a dict object maintains the order in which elements were inserted.

from pprint import pprint

d = defaultdict(None, {
    'Category 1': [
        {'id': 24, 'name': 'Video 1', 'cat_id': 16, 'tut_id': 14, 'description': ''}, 
        {'id': 28, 'name': 'Video 5', 'cat_id': 16, 'tut_id': 14, 'description': ''}
    ], 
    'Category 3': [
        {'id': 25, 'name': 'Video 3', 'cat_id': 18, 'tut_id': 14, 'description': ''}, 
        {'id': 29, 'name': 'Video 6', 'cat_id': 18, 'tut_id': 14, 'description': ''}
    ], 
    'Category 2': [
        {'id': 26, 'name': 'Video 2', 'cat_id': 17, 'tut_id': 14, 'description': ''}, 
        {'id': 27, 'name': 'Video 4', 'cat_id': 17, 'tut_id': 14, 'description': ''}
    ]
})

dicts = []
for v in d.values():
    dicts.extend(v)

pprint(dicts)

Output:

[{'cat_id': 16, 'description': '', 'id': 24, 'name': 'Video 1', 'tut_id': 14},
 {'cat_id': 16, 'description': '', 'id': 28, 'name': 'Video 5', 'tut_id': 14},
 {'cat_id': 18, 'description': '', 'id': 25, 'name': 'Video 3', 'tut_id': 14},
 {'cat_id': 18, 'description': '', 'id': 29, 'name': 'Video 6', 'tut_id': 14},
 {'cat_id': 17, 'description': '', 'id': 26, 'name': 'Video 2', 'tut_id': 14},
 {'cat_id': 17, 'description': '', 'id': 27, 'name': 'Video 4', 'tut_id': 14}]

From what I inferred, the category name is not required in the output.
After reshaping the data in this format, the required data can be extracted using a for loop.

video_id = 3
cat_id = 18
cat_name = "Category 3"
tut_id = 14


idx = -1
found = False
while(idx < len(dicts)):
    idx += 1
    video_id_i = int(dicts[idx]['name'].split()[1])
    if video_id == video_id_i:
        found = True
        break

# TODO: check for index out of bounds
if found:
    prev = dicts[idx - 1]
    curr = dicts[idx]
    next = dicts[idx + 1]

    print(prev, curr, next, sep='\n')

Output:

{'id': 28, 'name': 'Video 5', 'cat_id': 16, 'tut_id': 14, 'description': ''}
{'id': 25, 'name': 'Video 3', 'cat_id': 18, 'tut_id': 14, 'description': ''}
{'id': 29, 'name': 'Video 6', 'cat_id': 18, 'tut_id': 14, 'description': ''}
Diptangsu Goswami
  • 5,554
  • 3
  • 25
  • 36