user9431057
user9431057

Reputation: 1253

Re-order or Recreate Dictionary keys after a conditions for dictionary values -

I have the following dictionary,

test_dict = {1: 'Okay', 2: 'not good', 3: 'not well', 4: 'fine'}

From this, I want to check if a value's length is greater than 4 and if it is, then split into evenly sized chunks. As soon as the chunks happen, I want to add a key and value those chunks. For example, output like this:

out_dict = {1:'Okay', 2: 'not ', 3: 'good', 4: 'not ', 5: 'well', 6: 'fine'}

If you see since test_dict's key 2 and 3 had strings greater than 4, they get split and each split should get its own key and split value as values for keys 2,3,4,5.

This is what I have tried so far,

list_of_no_change = []
list_of_changed_dicts = []

for k, v in test_dict.items():    
    no_change = {}
    temp_dict = {}
    
    if len(v) > 4:
        # Divide into chunks
        chunks = [v[i:i + 4] for i in range(0, len(v), 4)]
        
        k_increment = 1
        
        for ix, vl in enumerate(chunks):
            
            if ix == 0:
                temp_dict[k] = vl
                
            else:
                new_k = k + k_increment
#                 print('Senetence id::>>>',ix, 'Value::>>>',vl, 'new key value::>>>',new_k)
                temp_dict[new_k] = vl
                k_increment +=1
        
    else:
        no_change[k] = v
        
    list_of_changed_dicts.append(temp_dict)
    list_of_no_change.append(no_change)

The output I get from both lists, which not close to where I am heading :(

list_of_no_change - [{1: 'Okay'}, {}, {}, {4: 'fine'}]
list_of_changed_dicts - [{}, {2: 'not ', 3: 'good'}, {3: 'not ', 4: 'well'}, {}]

Any help/suggestions to achieve my output would be great.

Upvotes: 0

Views: 53

Answers (2)

    test_dict = {1: 'Okay', 2: 'not good', 3: 'not well', 4: 'fine', 5: 'not good'}
    new_dict = {}
    no_of_chunks = 0
    values_greater_size = 0
    for key, val in test_dict.items():
        if len(val) > 4:
            chunks = [val[i:i + 4] for i in range(0, len(val), 4)]
            for new_val in chunks:
                new_dict.update({key + no_of_chunks - values_greater_size: new_val})
                no_of_chunks = no_of_chunks + 1
            values_greater_size = values_greater_size + 1
        else:
            new_dict.update({key + no_of_chunks - values_greater_size: val})
    print(new_dict)

Upvotes: 0

wjandrea
wjandrea

Reputation: 32997

I'm not sure what you're going for with the two lists, but the main problem is that you're simply not counting correctly. The easiest way to fix this is by chunking unconditionally, then using enumerate() to construct the output.

chunks = (v[i:i+4] for v in test_dict.values() for i in range(0, len(v), 4))
out_dict = {i: s for i, s in enumerate(chunks, 1)}
# {1: 'Okay', 2: 'not ', 3: 'good', 4: 'not ', 5: 'well', 6: 'fine'}

Here I made chunks a generator expression to avoid constructing a list.

I'm assuming you're using Python 3.7+, where dicts preserve insertion order. If not, you could replace test_dict.values() with (test_dict[k] for k in sorted(test_dict)).

Upvotes: 1

Related Questions