Home > Blockchain >  How can I check that a value exists in a dictionary or not in python
How can I check that a value exists in a dictionary or not in python

Time:10-12

mydict = {}

while True:
    num = int(input())
    if num == 0 :
        break
    while num > 0:
        inp = input().split(" ")
        mylist = list(inp)

        for i in range(0,len(mylist)):

            if mylist[i] in mydict.values():
                print(f"Yes, Value: '{mylist[i]}' exists in dictionary")
            else:
                print(f"No, Value: '{mylist[i]}' does not exists in dictionary")
                mydict[mylist[i]] = 0

        print(mydict)

    
        for i in range(0,len(mylist)):
            if i == 0 :
                mydict[mylist[i]]  = 3
            if i == 1 :
                mydict[mylist[i]]  = 2
            if i == 2 :
                mydict[mylist[i]]  = 1

        print(mydict)
        num-=1

this is my code and I don't know why it doesn't understand that some value in the dictionary is already exist after adding them, and I don't want them to change it to zero

2
3 3 2 1
No, Value: '3' does not exists in dictionary
No, Value: '3' does not exists in dictionary
No, Value: '2' does not exists in dictionary
No, Value: '1' does not exists in dictionary
{'3': 0, '2': 0, '1': 0}
{'3': 5, '2': 1, '1': 0}
3 2 3 1
No, Value: '3' does not exists in dictionary
No, Value: '2' does not exists in dictionary
No, Value: '3' does not exists in dictionary
No, Value: '1' does not exists in dictionary
{'3': 0, '2': 0, '1': 0}
{'3': 4, '2': 2, '1': 0}
0

but I want :

2            
3 3 2 1
No, Value: '3' does not exists in dictionary
No, Value: '3' does not exists in dictionary
No, Value: '2' does not exists in dictionary
No, Value: '1' does not exists in dictionary
{'3': 0, '2': 0, '1': 0}
{'3': 5, '2': 2, '1': 0}
3 2 3 1
Yes, Value: '3' exists in dictionary
Yes, Value: '2' exists in dictionary
Yes, Value: '3' exists in dictionary
Yes, Value: '1' exists in dictionary
{'3': 5, '2': 2, '1': 0}
{'3': 9, '2': 4, '1': 0}
0

CodePudding user response:

Your requirements are pretty unclear but it seems that this is something you want (explanation in code comments):

# import zip_longest because it allows to zip shorter
# and longer iterators and replace the corresponding
# shorter iterator's values with None by default
from itertools import zip_longest

# define the main dictionary
dct = dict()
# this is the placement so that first
# gets added a value of 3 and so on
# this is why zip longest is needed
placement = [3, 2, 1]

# main loop
while True:
    # get input and .split it
    inp = input().split()
    # if no input was provided
    # stop the program
    if not len(inp):
        break
    
    # and now for the input and the placement, zip longest them
    for key, added_value in zip_longest(inp, placement):
        # first check if the key is in the dictionary
        if key in dct.keys():
            # if the key is in dictionary print this and already add the
            # corresponding value depending in which place
            # the key was located in input
            print(f'Key {repr(key)} is in the dictionary')
            if added_value is not None:
                dct[key]  = added_value
        # in case user input is shorter just break this, so that
        # dictionary doesn't have None keys
        elif key is None:
            break
        else:
            # if the key wasn't in the dictionary set it to the 
            # value corresponding to placement
            print(f'Key {repr(key)} is not in the dictionary')
            dct[key] = added_value if added_value is not None else 0
    
    # print the current dictionary
    print(dct)
  • Related