'convert Element of list value in Dictionary Python

I have a data like that

[{'point1': ['20.900', '15.300', '20.400'], 
  'point2': ['0.600', '34.700', '8.100'], 
  'point3': ['12.100', '15.800', '2.300'], 
  'point4': ['15.000', '5.800', '16.900']}]

How can I convert the numbers into integers?



Solution 1:[1]

You could use a loop:

for d in lst:
    for v in d.values():
        for i, num in enumerate(v):
            v[i] = int(float(num))

print(lst)

Output:

[{'point1': [20, 15, 20],
  'point2': [0, 34, 8],
  'point3': [12, 15, 2],
  'point4': [15, 5, 16]}]

Solution 2:[2]

a similar question asked already check that too! you can do this also:

arr = [{
    'point1': ['20.900', '15.300', '20.400'], 
    'point2': ['0.600', '34.700', '8.100'], 
    'point3': ['12.100', '15.800', '2.300'], 
    'point4': ['15.000', '5.800', '16.900'],
    }]

[{k : list(map(float, v))  for k, v in point.items() } for point in arr]

Solution 3:[3]

array=[{'point1': ['20.900', '15.300', '20.400'], 'point2': ['0.600', '34.700', '8.100'], 'point3': ['12.100', '15.800', '2.300'], 'point4': ['15.000', '5.800', '16.900']}]
new_array=array[0]

for i in new_array.values():
    k=0
    for j in i:
        i[k]=int(float(j))
        k=k+1
        
print(new_array)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Peter Trcka
Solution 3 justjokingbro