[Introduction to Udemy Python3 + Application] 25. Dictionary-type method

** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **

■ Dictionary type method

.keys and .values
>>> d = {'x': 10, 'y': 20}
>>> d
{'x': 10, 'y': 20}
>>> d.keys()
dict_keys(['x', 'y'])
>>> d.values()
dict_values([10, 20])

Use .keys to enter the key in the dictionary, You can display the values in the dictionary with .values.

.update

>>> d = {'x': 10, 'y': 20}
>>> d2 = {'x':1000, 'z': 500}
>>> d.update(d2)
>>> d
{'x': 1000, 'y': 20, 'z': 500}

The value of x has been updated and the value of z has been added.

.get and .pop
>>> d = {'x': 1000, 'y': 20, 'z': 500}
>>> d['x']
1000

Of course, if you specify a key with [], the value of that key will be returned.

>>> d = {'x': 1000, 'y': 20, 'z': 500}
>>> d.get('x')
1000
>>> d
{'x': 1000, 'y': 20, 'z': 500}

If you specify the key with .get, you can retrieve the value. At this time, the contents of d have not changed.

>>> d = {'x': 1000, 'y': 20, 'z': 500}
>>> d.pop('x')
1000
>>> d
{'y': 20, 'z': 500}

You can also retrieve the value using .pop, Unlike .get, the retrieved data disappears from d.

◆ When a key that does not exist is specified
>>> d = {'x': 1000, 'y': 20, 'z': 500}
>>> d['a']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'a'
>>> d.get('a')
>>> n = d.get('a')
>>> type(n)
<class 'NoneType'>

If you do d ['a'], an error will occur, but If you use d.get ('a'), it will be NoneType.

.clear

>>> d = {'x': 1000, 'y': 20, 'z': 500}
>>> d.clear()
>>> d
{}

Using .clear () clears all the data in the dictionary.

del

>>> d = {'x': 1000, 'y': 20, 'z': 500}
>>> del d['x']
>>> d
{'y': 20, 'z': 500}

Of course, del can also be used.

in

>>> d = {'x': 1000, 'y': 20, 'z': 500}
>>> 'x' in d
True
>>> 'a' in d
False

With 'x' in d "Does 'x' exist in d?" Will be.

Recommended Posts

[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Udemy Python3 + Application] 29. Set method
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Udemy Python 3 + Application] Summary
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Udemy Python3 + Application] 33. if statement
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Udemy Python 3 + Application] 10. Numerical values
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 41. Input function
[Introduction to Udemy Python3 + Application] 17. List operation
[Introduction to Udemy Python3 + Application] 65. Exception handling
[Introduction to Udemy Python3 + Application] 11. Character strings
[Introduction to Udemy Python3 + Application] 44. range function
[Introduction to Udemy Python3 + Application] 46. Zip function
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[Introduction to Udemy Python3 + Application] 8. Variable declaration
[Introduction to Udemy Python3 + Application] 16. List type
[Introduction to Udemy Python3 + Application] 61. Dictionary comprehension
[Introduction to Udemy Python 3 + Application] 22. Tuple unpacking
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[Introduction to Udemy Python3 + Application] 23. How to use tuples
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Introduction to Udemy Python 3 + Application] 38. When judging None
[Introduction to Udemy Python3 + Application] 40.while else statement
[Introduction to Udemy Python3 + Application] 62. Set comprehension notation
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Udemy Python3 + Application] 43. for else statement
[Introduction to Udemy Python3 + Application] 67. Command line arguments
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
[Introduction to Udemy Python3 + Application] 14. Character substitution 15.f-strings
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
[Introduction to Udemy Python3 + Application] 52. Tupleization of positional arguments
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Udemy Python3 + Application] 32.1 When one line becomes long
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
[Introduction to Udemy Python3 + Application] 49. Function citation and return value declaration
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
Introduction to Monte Carlo Method