Python iteration related summary

enumerate function

If you want to get the index number at the same time as iterating, use the enumerate function.

names = ["kero", "Fire", "FLOG", "GECO"]

for i, name in enumerate(names):
    print(i, name)

0 kero 1 Fire 2 FLOG 3 GECO

Specify the index start number

for i, name in enumerate(names, 1):
    print(i, name)

1 kero 2 Fire 3 FLOG 4 GECO

for i, name in enumerate(names, 10):
    print(i, name)

10 kero 11 Fire 12 FLOG 13 GECO

zip function

You can iterate over multiple lists at the same time using the zip function.

names = ["kero", "Fire", "FLOG", "GECO"]
ages = [39, 21, 32, 45]

for age, name in zip(ages, names):
    print("Name: {0} Age: {1}".format(name, age))

Name: kero Age: 39 Name: Fire Age: 21 Name: FLOG Age: 32 Name: GECO Age: 45

When the number of elements is different

If the number of elements of each sequence object is different, it can be adjusted to a smaller number of elements.

names = ["kero", "Fire", "FLOG", "GECO"]
ages = [39, 21, 32]

for age, name in zip(ages, names):
    print("Name: {0} Age: {1}".format(name, age))

Name: kero Age: 39 Name: Fire Age: 21 Name: FLOG Age: 32

Iterative process of dictionary

data = { 'Flog':41, 'Geco':28, 'KERO':32 }

#List keys and values at the same time
for key, value in data.items():
    print(key, value)

Flog 41 KERO 32 Geco 28

#Enumerate keys
for key in data.keys():
    print(key)

Flog KERO Geco

#Enumerate values
for value in data.values():
    print(value)

41 32 28

Recommended Posts

Python iteration related summary
python related summary
Python Summary
Python summary
Python tutorial summary
Python basics summary
Kaggle related summary
Summary about Python scraping
Python Django tutorial summary
Python function argument summary
Python directory operation summary
Python AI framework summary
Summary of Python arguments
I / O related summary of python and fortran
Image related command summary
[Note] Python calculation acceleration (maybe) module "Numba" related summary
Python3 programming functions personal summary
Summary of python file operations
Summary of Python3 list operations
What's new in Python 3.10 (Summary)
Standard input / summary / python, ruby
Python class member scope summary
Python web programming article summary
python pandas study recent summary
Python Competitive Programming Site Summary
Python data type summary memo
Face detection summary in Python
Python Iteration Learning with Cheminformatics
What's new in Python 3.9 (Summary)
Python Crawling & Scraping Chapter 4 Summary
Virtual Environment Version Control Summary Python
Python
R / Python coding rule link summary
A brief summary of Python collections
Machine learning summary by Python beginners
selenium case study summary python pyCharm
Python package management tool personal summary
Python os related files, subdirectory operations
Python parallel / parallel processing sample code summary
Django static file (static) related settings summary
[Introduction to Udemy Python 3 + Application] Summary
Summary of Python indexes and slices
[OpenCV; Python] Summary of findcontours function
[Python] Summary of how to use pandas
IQ Bot Custom Logic Related Processing Summary
Python + Selenium Frequently used operation method summary
[Python] Summary of array generation (initialization) time! !! !!
"Python Machine Learning Programming" Summary Note (Jupyter)
Learn Python and AI related English words. .. ..
[Python] REST API essential, convenient library summary
[Python2.7] Summary of how to use unittest
Recommended books by 3 types related to Python
Summary of built-in methods in Python list
Summary of useful techniques for Python Scrapy
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
Axis option specification summary of Python "numpy.sum (...)"
Python URL and query parameter parsing summary
[Road to intermediate Python] Article link summary