[Python] Understand list slicing operations in seconds

at first

I think it will explain how I read slices. If you are a beginner and want to use Sassa and Slice, please read it.

When I googled "How to use python slices", all the articles were written in detail, so I will write them roughly.

Commentary

All you have to do is keep 3 points.

1. Understanding the index

Imagine that the index points between characters, with the first character at the left end being 0.

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

2. How to specify → List name [from: to: interval]

You can read the code in your head like a comment.

>>> num_list = [1, 5, 2, 3, 4]
>>> num_list[1:4:1] #Specify the index introduced in 1. Select 1st "from" 4th "to" with 1 "interval"
[5, 2, 3]

3. When there is only one ":", the ":" between "to:"

>>> num_list = [1, 5, 2, 3, 4]
>>> num_list[-4:] #Select "from" 4th behind
[5, 2, 3]

Now you can roughly read the code.

practice

After that, please read while holding down two examples. You should be able to cover how to write.

>>> num_list = [1, 5, 2, 3, 4]
>>> num_list[:4:1] #If "from" is omitted, select from the first element
[1, 5, 2, 3]
>>> num_list[1::1] #If "to" is omitted, the last element is selected.
[5, 2, 3, 4]
>>> num_list[1:4:] #Select by 1 interval by omitting "Interval"
[5, 2, 3]
>>> num_list[1:4] #":" Of "to:" (If you omit "interval", you can omit the second colon)
[5, 2, 3]
>>> num_list[-4:] #Select from the 4th behind
[5, 2, 3, 4]

At the end

I'm sorry if it's hard to understand.

Recommended Posts

[Python] Understand list slicing operations in seconds
Sorted list in Python
File operations in Python
Filter List in Python
Summary of Python3 list operations
Four arithmetic operations in python
Wrapping git operations in Python
I understand Python in Japanese!
Getting list elements in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Perform Scala-like collection operations in Python
ORC, Parquet file operations in Python
Delete multiple elements in python list
[Python] list
Display a list of alphabets in Python 3
Just print Python elapsed time in seconds
[Python] Pandas to fully understand in 10 minutes
Download images from URL list in Python
Summary of built-in methods in Python list
Get the EDINET code list in Python
[Python3] Understand the basics of file operations
Quadtree in Python --2
Python in optimization
[Python / DynamoDB / boto3] List of operations I tried
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Python basics: list
Geocoding in python
SendKeys in Python
[Python] Sort the list of pathlib.Path in natural sort
How to clear tuples in a list (Python)
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Make a copy of the list in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
View drug reviews using a list in Python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
Python> Comprehension / Comprehension> List comprehension
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Disassemble in Python
Reflection in Python
Randomly select elements from list (array) in python
Constant in python
format in python
Scons in Python3
Puyo Puyo in python
Sort list elements in a specified order in Python