How to check if a value exists in a dictionary ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AltoWaltz

Hello,

I have an external dictionary imported as a singleton DataImport.inven_data with the following contents:

print(DataImport.inven_data)
{101:[LeatherBoots, 1], 202:[Sword, 1]}

101 and 202 are keys and [LeatherBoots, 1] [Sword, 1] are values with the item name and item quantity. I want to get a true false boolean if a value Sword is present in a dictionary and have problems setting this up.

Any help appreciated.

:bust_in_silhouette: Reply From: klaas

Hi,
this does not seem a very efficent setup (at least when the keys have no other use). You have to step through the dict and search a match.

func search( what):
   for item in dict:
      if item[0] == what:
         return item

I think item in dict will give you the key, not the value, so item[0] wont contain Sword… at least i tried that and that was the result.

p7f | 2020-09-15 13:01

:bust_in_silhouette: Reply From: p7f

If you want to look something up in a dictionary, you should use that as key, instead of a number like 202 or 101. If you know sword is always going to be 202, you could ask

if 202 in DataImport.inven_data:
    print(DataImport.inven_data[202]) #replace this by your actual code

That will print [Sword, 1]
If you really need to have the key 202, but need to look if some of the values is Sword, it dependes what is sword… if you have on the sword script something like class_name Sword maybe you could do a for loop like this:

for item in DataImport.inven_data:
   if DataImport.inven_data[item][0] == "Sword":
		print(dict[item]) #replace this by your actual code
		break

That would also only print [Sword, 1]
Is something like that what you are looking for?

201 -210 are keys that get populated randomly by the order of how the player picks up random loot.

So 202 will contain different things all the time. Should have mentioned this now that I think about it.

I think I will try setting up if statements checking if any item from 201 - 210 has combo “key number, Sword”, but that will be a lot of text.

AltoWaltz | 2020-09-15 13:09

And the loop i wrote in the answer doesnt work for you?

p7f | 2020-09-15 13:16

I get Invalid operands ‘String’ and ‘Array’ in operator ‘==’.

I tried with:

for i in DataImport.inven_data.values():
	if DataImport.inven_data[i][0] == [Sword]:
		print(i)

which returns first item in the dictionary which is not what I am looking for. The code below returns the values of the dictionary, I just cant figure it out how to approach single line returning boolean if the item value is there so I can do some further code.

for i in DataImport.inven_data.values():
	print(i)

AltoWaltz | 2020-09-15 14:41

Sorry, that was why i asked if you had a class_name Sword or something
What is sword? an array?

p7f | 2020-09-15 14:45

Sword and other loot dictionary & inventory items are defined within json and most don’t have their own class, but some do. Sorry, this is the best as I can explain myself as this is my first godot project.

AltoWaltz | 2020-09-15 14:58

:bust_in_silhouette: Reply From: 1shevelov

I would use item name as a property name if it’s unique. Your records will look like this:

inven_data = {
    "LeatherBoots": {
        "num": 1,                 # number of items
        "inven_loc": 101},   # id of inventory place
    "Sword": {
        "num": 2,
        "inven_loc": 202}
}

and check for item with simple if inven_data.has("Sword")

or an Array for inventory where each member is a Dictionary:

inven_data = []
inven_data.resize(INVENTORY_SIZE)
inven_data[inv_index] = {"name": "Sword", "quantity": 1}

looking for records a bit more complicated then:

for each in inven_data:
    if !each.empty():   # check if inventory place has any item
        if each.name == "Sword":
            return true
:bust_in_silhouette: Reply From: Lightning_A
if value in dict.values():
   ...

Dictionary.values()
Array.has()