[Python] Comprehension notation. Write a for statement simply. (A collection is the difference between an iterator and an iterator)

[Python] Comprehension notation. Write a for statement simply. (A collection is the difference between an iterator and an iterator)

You can create a list, set type, etc. (collection) using a for statement.

What is a comprehension?

How to write a for statement easily.

[Expression for variable in iterable] └ If you enclose it in [], the output type will be list. └ {} is a set type

[(i*2) for i in range(5)]

#output
[0, 2, 4, 6, 8]

The output is in list format. Same as below.

python


arrs=[]
for i in range(5):
    arr = (i*2)
    arrs.append(arr)
arrs

#output
[0, 2, 4, 6, 8]

▼ {} is a set type

{(i*2) for i in range(5)}

#output
{0, 2, 4, 6, 8}

## Supplement ### ■ What is a collection? A type of data type. A general term for types that can have multiple elements.

list, range, set, etc.

Str, int, float, etc. that can only have one data are not collections.

■ Difference between iterator and iterator

Iterable is a general term for repeatable objects. Iterators are a type of iterator and are more limited.

** ・ Iterable ** A repeatable object. Objects of type list, tapple, range.

"For i in iterable"


** ・ Iterator ** A type of mold. (Original type_iterator) Become an iterator with the iter function. You can use the next function.

** ▼ Confirmation of iterator type **

For list


arr = [1,2,3,4,5]

iterArr = iter(arr)
type(iterArr)

#list_iterator

For tuple


brr = (1,2,3,4,5)

iterArr = iter(arr)
type(iterArr)

#tuple_iterator

For range


crr = range(5)

iterArr = iter(arr)
type(iterArr)

#range_iterator

** ・ next function ** Extract the elements of type iter one by one. irreversible.

next (iterator)

Iterator & next


arr = [1,2,3,4,5]
iterArr = iter(arr)

next(iterArr)
1

next(iterArr)
2
~
~
~
next(iterArr)
5

next(iterArr)
StopIteration: #← An error will occur if all are extracted

If you use an iterator and next, you can process in order.

▼ Iterators can also use for statements. Iterators are one of the iterator elements.

python


arr = [1,2,3]
iterArr = iter(arr)

for i in iterArr:
    print(i)

1
2
3

Recommended Posts

[Python] Comprehension notation. Write a for statement simply. (A collection is the difference between an iterator and an iterator)
[Introduction to Python] What is the difference between a list and a tuple?
About the difference between "==" and "is" in python
What is the difference between a symbolic link and a hard link?
I want to absorb the difference between the for statement on the Python + numpy matrix and the Julia for statement
Difference in how to write if statement between ruby ​​and python
What is the difference between `pip` and` conda`?
The answer of "1/2" is different between python2 and 3
What is the difference between Unix and Linux?
What is the difference between usleep, nanosleep and clock_nanosleep?
[Python] Explain the difference between strftime and strptime in the datetime module with an example
Turn an array of strings with a for statement (Python3)
Difference between ps a and ps -a
Write a script in Shell and Python to notify you in Slack when the process is finished
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
[Introduction to Python] How to use the in operator in a for statement?
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between list () and [] in Python
Write a C language linked list in an object-oriented style (with code comparison between Python and Java)
Difference between python2 series and python3 series dict.keys ()
What is the python underscore (_) for?
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
[Python] Create a linebot to write a name and age on an image
I measured the speed of list comprehension, for and while with python2.7.
difference between statements (statements) and expressions (expressions) in Python
Write the test in a python docstring
Difference between @classmethod and @staticmethod in Python
Change the list in a for statement
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[python] [meta] Is the type of python a type?
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
[Python] Concatenate a List containing numbers and write it to an output file.
[Python] A program that calculates the difference between the total numbers on the diagonal line.
Turn multiple lists with a for statement at the same time in Python
The place to be evaluated may be different between map and list comprehension notation
[Introduction to Python] How to get the index of data with a for statement