Python3> slice copy / slice notation> used in for statements, etc.

http://docs.python.jp/3/tutorial/controlflow.html#for-statements

If you need to modify an iterating sequence inside a loop (for example, to duplicate a selected item), it's a good idea to make a copy first. Iterations to sequences do not implicitly make a copy. Slice notation makes this especially useful:

>>> for w in words[:]:  # Loop over a slice copy of the entire list.
...     if len(w) > 6:
...         words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']

In the above example, the story gets confusing if the list itself changes while turning in an iterator. I understood that it would be better to use slice notation in such cases (maybe wrong).

Recommended Posts

Python3> slice copy / slice notation> used in for statements, etc.
Summary of various for statements in Python
Functions that can be used in for statements
Class notation in Python
Tool to make mask image for ETC in Python
Search for strings in Python
Techniques for sorting in Python
Convert CIDR notation in Python
Slice error in python (´ ; ω ; `)
About "for _ in range ():" in python
Insert Import statements needed for Python code completion in Neovim
Useful tricks related to list and for statements in Python
What is Python? What is it used for?
Check for external commands in python
8 Frequently Used Commands in Python Django
Included notation in Python function arguments
[Python] Basic knowledge used in AtCoder
[TouchDesigner] Tips for for statements using python
Run unittests in Python (for beginners)
Display LaTeX notation formulas in Python, matplotlib
Implement __eq__ etc. generically in Python class
difference between statements (statements) and expressions (expressions) in Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
Two rules when reading Python (slice notation)
Tips for dealing with binaries in Python
[python] Frequently used techniques in machine learning
Type annotations for Python2 in stub files!
Precautions when using for statements in pandas
[Python] Organizing how to use for statements
Process multiple lists with for in Python
MongoDB for the first time in Python
Get a token for conoha in python
Sample for handling eml files in Python
AtCoder cheat sheet in python (for myself)
I searched for prime numbers in python
Tips for making small tools in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
Python slice
CERTIFICATE_VERIFY_FAILED in Python 3.6, the official installer for macOS
Python basics (variables, types, operators, if statements, for statements)
Import-linter was useful for layered architecture in Python
Unattended operation of Google Spreadsheets (etc.) in Python
Techniques often used in python short coding (Notepad)
A program that removes duplicate statements in Python
Add quotation marks ">" for replying emails in Python3
Make a copy of the list in Python
Correct half-width and full-width notation fluctuations in Python
Logging settings for daily log rotation in python
Tips for hitting the ATND API in Python
Display candlesticks for FX (forex) data in Python
Boost.NumPy Tutorial for Extending Python in C ++ (Practice)
Zero padding for dynamic variable values in Python
Try to calculate RPN in Python (for beginners)
Notes for implementing simple collaborative filtering in Python
Copy data between Google Keep accounts in Python
Don't use readlines () in your Python for statement!
[Python] f strings should be used for embedding strings
Summary of frequently used Python arrays (for myself)
Code often used in Python / Django apps [prefectures]
Settings for Python coding in Visual Studio Code