I want to use both key and value of Python iterator

When using dict as an iterator in a for statement, there are times when you want to use both key and value. Also index when list is used as a lata.

I'll write it down every time, so I'll write it down.

By the way, I used to do this ↓

python3


>>> dict = {'a': 10, 'b': 20, 'c': 30}
>>> for key in dict.keys():
>>>    print('key: ', key, ' ,value: ', dict[key])
key:  a  ,value:  10
key:  b  ,value:  20
key:  c  ,value:  30

It seems that if you make two variables after for and pass dict.items (), key and value will be entered in each.

python3


>>> dict = {'a': 10, 'b': 20, 'c': 30}
>>> for key, value in dict.items():
>>>    print('key: ', key, ' ,value: ', value)
key:  a  ,value:  10
key:  b  ,value:  20
key:  c  ,value:  30

Use enumerate (list) if you want to use index and value in list

python3


>>> l = ['a', 'b', 'c']
>>> for index, value in enumerate(l):
>>>    print('index: ', index, ' ,value: ', value)
index:  0  ,value:  a
index:  1  ,value:  b
index:  2  ,value:  c

You can do the same with pandas Series and DataFrame, but the value set in index will not be applied and index = 0,1,2, ...

By the way, in the map of js, the same thing is written in a split assignment.

Recommended Posts

I want to use both key and value of Python iterator
I want to know the features of Python and pip
I want to use jar from python
I want to use Temporary Directory with Python2
I want to use ceres solver from python
I tried to summarize how to use matplotlib of python
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to use the R dataset in python
How to store Python function in Value of dictionary (dict) and call function according to Key
I want to initialize if the value is empty (python)
I want to specify another version of Python with pyvenv
I want to start a lot of processes from python
I want to use only the normalization process of SudachiPy
[Python] Summary of how to use split and join functions
[Python] I want to use the -h option with argparse
Comparison of how to use higher-order functions in Python 2 and 3
I want to use VS Code and Spyder without anaconda! !! !!
I want to debug with Python
I want to use a wildcard that I want to shell with Python remove
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
[Introduction to Python] I compared the naming conventions of C # and Python.
I want to output the beginning of the next month with Python
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
Comparison of GCP computing services [I want to use it serverless]
[Python] Summary of how to use pandas
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
How to install and use pandas_datareader [Python]
I want to build a Python environment
I want to use Linux on mac
I want to analyze logs with Python
[Python2.7] Summary of how to use unittest
I want to use IPython Qt Console
python: How to use locals () and globals ()
How to use Python zip and enumerate
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
How to use is and == in Python
[Question] How to use plot_surface of python
Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-
(Python Selenium) I want to check the settings of the download destination of WebDriver
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
I want to color a part of an Excel string in Python
I tried to verify and analyze the acceleration of Python by Cython
I want to analyze the emotions of people who want to meet and tremble
I want to use the Qore SDK to predict the success of NBA players
I want to format and check Python code to my liking on VS Code
Python: I want to measure the processing time of a function neatly
I want to make a web application using React and Python flask
I want to revive the legendary Nintendo combination by making full use of AI and HR Tech!
I want to do Dunnett's test in Python
[Python] How to use two types of type ()
I want to memoize including Python keyword arguments
I want to create a window in Python
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to make a game with Python
Summary of how to use MNIST in Python
I want to merge nested dicts in Python
Output python log to both console and file