[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement

** * 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. ** **

■ Process the dictionary with a for statement

◆ Example

dict_for


d = {'x': 100, 'y': 200}

for v in d:
    print(v)

result


x
y

With such code, only the key is printed. I want to print not only the key but also the value.

.items method

dict_for


d = {'x': 100, 'y': 200}

for k, v in d.items():
    print(k,':', v)

result


x : 100
y : 200

By using .items (), if you prepare two variables, key and value will be assigned to each.

To find out how it works, try printing d.items ().

dict_for


d = {'x': 100, 'y': 200}

print(d.items())

# for k, v in d.items():
#     print(k,':', v)

result


dict_items([('x', 100), ('y', 200)])

You can see that a list of tuples is returned in the d.items () part. This shows that this tuple is unpacked and assigned to two variables.

Recommended Posts

[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
[Introduction to Udemy Python3 + Application] 43. for else statement
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Python] How to get the index of data with a for statement
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[Introduction to Udemy Python3 + Application] 61. Dictionary comprehension
[Introduction to Python] How to use the in operator in a for statement?
[Introduction to Udemy Python3 + Application] 40.while else statement
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
[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
How to convert an array to a dictionary with Python [Application]
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
[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] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 13. Character method
[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] 8. Variable declaration
[Introduction to Udemy Python3 + Application] 29. Set method
[Introduction to Udemy Python3 + Application] 16. List type
[Introduction to Udemy Python 3 + Application] 22. Tuple unpacking
[Introduction to Python] How to split a character string with the split function
[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 Python] How to sort the contents of a list efficiently with list sort
[Introduction to Udemy Python3 + Application] 62. Set comprehension notation
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
Take the free "Introduction to Python for Machine Learning" online until 4/27 application
Introduction to Python with Atom (on the way)
[Introduction to Udemy Python3 + Application] 67. Command line arguments
[Introduction to Python] How to write a character string with the format function
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
[Introduction to Udemy Python3 + Application] 14. Character substitution 15.f-strings
[Introduction to Python] What is the method of repeating with the continue statement?
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Turn multiple lists with a for statement at the same time in Python
[Introduction to Udemy Python3 + Application] 37. Techniques for determining that there is no value
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
[Introduction to Udemy Python 3 + Application] 66. Creating your own exceptions