Python basic course (10 inclusion notation)

Comprehension notation

Comprehension notation allows you to write list, set, and dictionary data operations in a short number of lines. It also speeds up processing, so use this if you have a huge number of elements.

List comprehension

First, execute the program that uses the following list comprehension notation.

list_comprehension1.py


result = [x**2 for x in [1, 2, 3, 4, 5]]
print("{0}".format(result))

x ** 2 means "x squared", and the list result contains the following values:

[1, 4, 9, 16, 25]

If you don't use list comprehensions, you can replace the above program with:

list_comprehension2.py


result = []
for i in [1, 2, 3, 4, 5]:
    result.append(i**2)
print("{0}".format(result))

** [** * Values to put in elements (using variables) * ** for ** * Variables * ** in ** * List * **] **

Lists can use list-type data like programs, and range () can also be used like range (1,6).

In list comprehension notation, you can specify the elements to be processed and the elements not to be processed in the elements of the list.

list_comprehension3.py


print("{0}".format( [x*10+1 for x in range(1,6) if x > 2 and x < 5]))

The output result is as follows.

[31, 41]

Since the list comprehension returns a list, it is of course possible to give the list comprehension directly to the print statement. If you don't use list comprehensions, you can replace the above program with:

list_comprehension4.py


result = []
for x in range(1,6):
    if x > 2 and x < 5:
        result.append(x*10+1)
print("{0}".format(result))

** [** * Values to put in elements (using variables) * ** for ** * Variables * ** in ** * List * * Conditions * **] **

Set comprehension

The inclusion notation can also be used as a set. The usage is the same as the list comprehension notation.

set_comprehension.py


print("{0}".format( {x*10+1 for x in range(1,6) if x > 2 and x < 5}))

The output result is as follows. set([41, 31])

Dictionary (dictionary) inclusion notation

Separating keys and values with: and enclosing keys: values with {} is the same as a normal dictionary. The usage is the same as the list.

dictionary_comprehension.py


print("{0}".format( {str(x):x**2 for x in range(1,6)}))

You can do the same thing without using comprehensions, but you can achieve simple and high-speed operation. A good source code will be created if you use it with the inclusion notation in mind.

Next: Python Basic Course (11 exceptions)

Recommended Posts

Python basic course (10 inclusion notation)
Python Basic Course (7 Dictionary)
Python basic course (9 iterations)
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python Basic Course (Introduction)
Python basic course (13 classes)
Python basic course (8 branches)
Python Basic Course (3 Python Execution)
Python Basic Course (5 List Tuples)
Python Basic Course (1 What is Python)
Python Basic Course (14 Modules and Packages)
Python basic operation 1st: List comprehension notation
RF Python Basic_01
Memorandum of python beginners About inclusion notation
Basic Python writing
Python3 basic grammar
Python Basic Course (at the end of 15)
RF Python Basic_02
Python basic course (4 numeric type / character string type)
Basic grammar of Python3 system (included notation)
Python I'm also basic
Python basic grammar / algorithm
Basic sorting in Python
Master the inclusion notation
[python] class basic methods
Python3 cheat sheet (basic)
Python basic grammar (miscellaneous)
Python basic memorandum part 2
python basic on windows ②
Python basic memo --Part 2
Basic Python command memo
Python basic grammar note (4)
Python basic grammar note (3)
Basic knowledge of Python
Python basic grammar memo
OpenCV basic code (python)
Python basic memo --Part 1
Class notation in Python
python memorandum super basic
Python basic if statement
Python Basic --Pandas, Numpy-
Python application: Pandas Part 1: Basic
BASIC authentication with Python bottle
Python basic dict sort order
[Python] Using OpenCV with Python (Basic)
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Convert CIDR notation in Python
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
[Python] [SQLite3] Operate SQLite with Python (Basic)
Basic usage of Python f-string
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Java and Python basic grammar comparison
Python Application: Data Cleansing Part 1: Python Notation
Included notation in Python function arguments
Scraping with Selenium in Python (Basic)