Write a Python program to replace the value of a particular key with nth index of value if the value of the key is list.


Topic: Write a Python program to replace the value of a particular key with nth index of value if the value of the key is list.

Solution

test_list = [{'tsai': [5, 3, 9, 1], 'is': 8, 'good': 10}, 
             {'tsai': 1, 'for': 10, 'geeks': 9}, 
             {'love': 4, 'tsai': [7, 3, 22, 1]}]   
N = 2
key = "tsai"  
for sub in test_list: 
    if isinstance(sub[key], list): 
        sub[key] = sub[key][N]



List all Python Programs