Answers for "integer key dictionary python"

1

convert dictionary keys to int python

d = {'1':'145' , '2':'254' , '3':'43'}
d = {int(k):int(v) for k,v in d.items()}#convert both keys and values to int
>>> d
{1: 145, 2: 254, 3: 43}
Posted by: Guest on January-24-2022
0

python create a dictionary of integers

>>> dict.fromkeys(range(1, 4))
{1: None, 2: None, 3: None}
>>> dict(zip(range(1, 4), range(1, 4)))
{1: 1, 2: 2, 3: 3}
Posted by: Guest on July-06-2020

Python Answers by Framework

Browse Popular Code Answers by Language