Make a dictionary in Python from input values

前端 未结 10 1395
情深已故
情深已故 2021-02-03 12:06

Seems simple, yet elusive, want to build a dict from input of [key,value] pairs separated by a space using just one Python statement. This is what I have so far:



        
10条回答
  •  野性不改
    2021-02-03 12:57

    n = int(input("enter a n value:"))
    d = {}
    
    for i in range(n):
        keys = input() # here i have taken keys as strings
        values = int(input()) # here i have taken values as integers
        d[keys] = values
    print(d)
    

提交回复
热议问题