I don't know much about dictionaries, so it's a memo. 2.7 is because MotionBuilder is the main environment in which I use Python.
in key in dictionary
dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print("key1" in dictionary)
#True
Bool returns whether the item with the specified key exists. key in dictionary.values()
dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print ("key1" in dictionary.values())
print ("value1" in dictionary.values())
#False
#True
Here, bool returns whether the specified Value exists.
dictionary[key]
dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print (dictionary["key1"])
#value1
```py
Slice is the acquisition with []
dictionary.get(key)
dictionary = {"key1":"value1","key2":"value2","key3":"value3"} print (dictionary.get("key1"))
#value1
There is no difference in the fact that the value can be obtained in both cases, but the behavior when there is no target is different.
Error is returned for slices, None is returned for get ()
```py
print (dictionary.get("key4"))
#None
It is also possible to prepare the return value in advance when the specified key does not exist
print (dictionary.get("key4","no value"))
#no value
keys()
dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print(dictionary.keys())
#['key3', 'key2', 'key1']
The value is retrieved in a list. I did some research to find out why it was obtained from the end, but I don't know. If anyone knows, please let me know.
value()
dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print(dictionary.values())
#['value3', 'value2', 'value1']
The value is also obtained in the list
items()
dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print(dictionary.items())
[('key3', 'value3'), ('key2', 'value2'), ('key1', 'value1')]
You can get key and value at the same time. Each element in the list is a tuple.
iteritems()
dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
for k, v in dictionary.iteritems():
print k,v
#key3 value3
#key2 value2
#key1 value1
You can also get the key and value at the same time. It seems to use this when turning with a for statement. Items () of python3 series seems to return here by default.
I was not good at dictionary type, but I feel that I was able to organize it a little by myself by putting it together in this way. I want to check again the case where the value is obtained from the end.
https://qiita.com/jinshi/items/9f0f7ae716bce77a1b8a https://www.headboost.jp/python-dict-keys-tips/