Commonly used methods for dictionaries
For convenience, for example, first create 2 dictionaries
list_test={"bob":19,"aoa":18,"coc":17} list_b={'qqq':000}
parameters | return value | hidden meaning |
---|---|---|
.items() | dict_items([(‘bob’, 19), (‘aoa’, 18), (‘coc’, 17)]) | Returns all key values |
.keys() | dict_keys([‘bob’, ‘aoa’, ‘coc’]) | Returns the keys value |
.values() | dict_values([19, 18, 17]) | Returns the value of values |
.clear() | not have | Delete all items in the dictionary |
.get(key) | 19 | Returns the value corresponding to the key in the dictionary.For example list_test.get("bob")
|
list_test.pop(key) | 19 | removes and returns the value corresponding to the key in the dictionary.For example list_test.pop("bob")
|
list_test.update(dictionary) | {‘bob’: 19, ‘aoa’: 18, ‘coc’: 17, ‘qqq’: 0} | Merge dictionary list_test with list_b.For example list_test.update(list_b)
|
Outputs the values in the dictionary:
list_test={"bob":19,"aoa":18,"coc":17} for i,j in (): print(i, j)
list_test={"bob":19,"aoa":18,"coc":17} for i in test : print(i,test[i])
Deleting an entry in the dictionary
del list_test["coc"]
Whether the value is in the dictionary (in, not in), return True/False
'bob' in list_test
PS: Other methods
person = {'name':'xiaoming', 'age':18} # The first throws a KeyError exception if the key does not exist. person['city'] # The second does not throw an exception and returns None if it doesn't exist, or you can set a default return value. ('city',"Shanghai.") # Third Similar to the second, except that the setdefault method updates the dictionary ('city', 'Shanghai')
to this article on Python to take out the dictionary value of the realization of the article is introduced to this, more related Python to take out the value of the dictionary content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!