Skip to content
Advertisement

How to add multiple values in dictionary for a key [closed]

I am trying to write a python code where i want to take multiple or 3 number inputs from user for one key in dictionary . Can anyone please help . Appreciate in advance

I think output will be as given below . For key “ABC” assigned/values are 67,68,69.

{ABC : [67, 68, 69]}

m=0
empty_dic = {}
numbers = []
name = input()
key = name
while m < 3:
    n = int(input())
    numbers.append(n)
    m += 1
empty_dic.setdefault(key, numbers)
empty_dic[key].append(numbers)
print(empty_dic)```

Advertisement

Answer

here if you want to add multiple numbers

a_list = []
a_dict = {'Number': a_list}

#here so the will loop will be runing
RUN = True

while RUN:
    a = int(input('Enter a number: '))
    l = a_list.append(a)
    quit_the_while = input('Enter q to quit or any thing to add more number: ')
    if 'q' in quit_the_while:
        break
    else:
        continue

print(a_dict)

here for 3 numbers only

a_list = []
a_dict = {'Number': a_list}

while len(a_list) < 3:
    a = int(input('Enter a number: '))
    l = a_list.append(a)

print(a_dict)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement