Closed moontr3 closed 3 months ago
You can get a value from a dict by using dict[x] with either index or a key.
dict[x]
dict = UDict(foo=1, bar=2) dict["foo"] # 1 dict[1] # 1 dict[-1] # 2
In inbuilt dicts, you can actually use ints as a key:
int
dict = {1: "foo", 2: "bar"} dict[1] # foo dict[2] # bar
So, if you do something like this, you will get the value by the key, not index:
dict = UDict({2: "foo", 1: "bar"}) dict[1] # bar
But what if you reeeeally want to get a value by an index, not a key? This definitely needs a fix.
I propose the following solution:
dict.get(index=1) # foo dict.get(key=1) # bar
And don't forget about the default feature of the inbuilt dict.get()
default
dict.get()
Thanks for your issue. I'll fix that soon
You can get a value from a dict by using
dict[x]
with either index or a key.In inbuilt dicts, you can actually use
int
s as a key:So, if you do something like this, you will get the value by the key, not index:
But what if you reeeeally want to get a value by an index, not a key? This definitely needs a fix.
I propose the following solution: