Is there a way to lookup a value in a dictionary?


Hello, guys!

I just have a question about this. When saying if X in directions is it similar (or can be called an iteration through the dictionary)?

Because if so, I see it works only if X is a key, not a value (regardless if that is a string or an integer):

Is it possible to add an if condition to target the values only? If so, which one is it? (Sorry, I tried searching for it but did not manage to find it).

You misunderstand what a dictionary is.
It’s a key-value store, if you have a key you can get the value. There is no searching involved, it is direct lookup.

There’s nothing stopping you from iterating through the values and comparing it to a value of your own. But that’s no different from using a list, it’s completely different from testing whether a key is present.

1 Like

Hello, @ionatan! Thank you for the in-depth explanation. :star_struck: However, by ‘searching’ and ‘it’ I was referring to the means, the way, the process, or the function not literally a searching within the dictionary.

And by this process I mean: if + the condition (the key + in + the name of the dictionary)

My question was if there is another way of pointing to the value (as easy as you do it with the key, by just mentioning it) in the expression, instead of just using .value() method.

Have a wonderful day! :hugs:

Testing for key membership is directly related to the purpose of dict, that’s what it provides, that’s why … it provides that operation

Linear search (testing each one) isn’t that. You don’t need dict to do that for you, if dict did this then it would be behaving like a list or, in other words, use a list. But since it does provide the values, you can do linear search on that.

Instead of having each thing do all possible things, we can combine operations to get the same effect without the duplicated behaviour.

The value that a dict’s value method returns however, that isn’t a dict and that has the behaviour you’re looking for.

There’s also no reason to shy away from changing data types, re-shaping your data can be thought of as a computation, there’s nothing wrong with that, if anything it probably makes your life easier if you’re willing to reshape things to fit the operations that you’re going to do. (At least, this makes sense as long as you’re planning to do something that requires touching the whole structure anyway. Dict is all about avoiding that.)

2 Likes

Perfect! Thank you a lot! I understand where the confusion I was making was. :+1: :star_struck: