Python tuple comprehensions and generator expressions

Make a note about the difference between tuple comprehension and generator formula

First of all, from the review of the comprehension ///

inner.py


list=[]
for x in range(10):
    list.append(x)
print(list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


#If you rewrite this as a comprehension

list2=[x for x in range(10)]
print(list2)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Now, about the tuple comprehension and generator formula of the main subject

tuple.py


#Ordinary iterator
def g():
    for i in range(4):
        yield i
g=g()
print(next(g))
print(next(g))
print(next(g))
print(next(g))
"""
0
1
2
3
"""


g=(i for i in range(4))#It sounds like a tuple, but it's a generator
print(type(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
"""
<class 'generator'>
0
1
2
3

"""







iterator.py


g=tuple(i for i in range(4))#For tuples, you need to write tuple before the parentheses
print(type(g))
print(g)
#<class 'tuple'>
#(0, 1, 2, 3)

Summary

In the generator formula, it should be described in (), whereas it should be described in ().

In the tuple comprehension, you must write tuple before ().

Recommended Posts

Python tuple comprehensions and generator expressions
Python comprehension (list and generator expressions) [additional]
Python list comprehensions and generators
About Python and regular expressions
Python higher-order functions and comprehensions
Effective Python Memo Item 9 Consider generator expressions for large comprehensions
Introductory Python Modules and conditional expressions
difference between statements (statements) and expressions (expressions) in Python
[Python Iroha] Difference between List and Tuple
EP 9 Consider Generator Expressions for Large Comprehensions
[Python] Generator function
Python> tuple> tuple unpacking
[Python] Regular Expressions Regular Expressions
[Python] How to use hash function and tuple.
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Batch design and python
Python iterators and generators
Writing using lambda expressions and filter functions and writing using list comprehensions
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
Generator comprehension notation Tuple comprehension notation
python input and output
Python and Ruby split
Implementing a generator using Python> link> yield and next ()> yield
Indent comprehensions in Python
Zundokokiyoshi (generator) in Python
[Python] Chapter 04-05 Various data structures (tuple creation and features)
Python3, venv and Ansible
Python asyncio and ContextVar
Get rid of dirty data with Python and regular expressions
Multiple regression expressions in Python
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
Story when iterating python tuple
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Combination of recursion and generator
Python and ruby slice memo
Python installation and basic grammar
I compared Java and Python!
Python shallow and deep copy
Use regular expressions in Python
About Python, len () and randint ()
About Python datetime and timezone
Install Python 3.7 and Django 3.0 (CentOS)
Wrap long expressions in python