#How do i get the index of an element from a dictionary?

6 messages · Page 1 of 1 (latest)

shy garnet
#

I am making a code that checks if the input string has the same name as one of the sub-dictionaries (is that what they're called?) and then I need it to set effectIndex to that sub-directionary's index.

In said dictionary, every value has a name, which is a string.

                    if element['name'] == cmdArray[1]:
                        #effectIndex = !element index!```
I hope I was clear!
queen charm
#

Just to make everything clear.. Please correct me if anything is off:

  • EFFECTS is your dictionary
  • each key in your dictionary.. has an array
  • each array has an effect name at the array index of 1
#

I'm a bit confused where your cmdArray is coming from

#

After further review, depending on what it is you are trying to achieve.. I would recommend one of two changes:

  1. You are attempting to just get an index as an int.. for some other process and you will not need to access the dictionary again.

In this case, if you just want a value that is effectively "X" = 1, consider setting up an enum.

  1. You are currently trying to save the index.. so you can access the dictionary again at a later point. If this is the case, I would recommend just getting the entire dictionary (er, sub-dictionary) and passing it where it needs to go. It'll make accessing its contents so much easier in the long run.
shy garnet
shy garnet