Home > Net >  How to change values in list of values in a dictionary inplace
How to change values in list of values in a dictionary inplace

Time:01-20

How to replace values in list of values inplace? From float to int.

some_dict = {"B1": [-1.0, 3.0], "B2": [-2.0, 4.0], "B3": [-3.0, 5.0], "B4": [-5, -6]}

Result :

some_dict = {"B1": [-1, 3], "B2": [-2, 4], "B3": [-3, 5], "B4": [-5, -6]}

CodePudding user response:

I will try:

{k: [int(v[0]), int(v[1])] for k, v in some_dict.items()}

CodePudding user response:

My opinion is just simply iterate over the dict items, then iterate over the values list:

for key, value in some_dict.items():
    for i in range(len(value)):
        value[i] = int(value[i])

CodePudding user response:

i think this will work or you:

new_dict = {k: [int(i) for i in v] for k, v in some_dict.items()}
  •  Tags:  
  • Related