This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: string comprehension
Type: enhancement Stage: resolved
Components: Versions: Python 3.10
process
Status: closed Resolution: later
Dependencies: Superseder:
Assigned To: Nosy List: alvarezdqal, rhettinger, zach.ware
Priority: normal Keywords:

Created on 2021-04-20 21:55 by alvarezdqal, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg391480 - (view) Author: David Alvarez Lombardi (alvarezdqal) Date: 2021-04-20 21:55
As of now the best way to filter a str is to convert to list, filter, then join back to a str. I think a a string comprehension would be very useful for this.


So to get only ascii_lower case chars given this string,
````
s = "a1b2c3d4"
````

I could do this
````
filtered_s = c"ch for ch in s if ch in string.ascii_lowercase"
````

instead of this.
````
s_list = []
for i in range(len(s)):
    if s[i] in string.ascii_lowercase:
        s_list.append(s[i])

filtered_s = "".join(s_list)
````
msg391481 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2021-04-20 22:07
filtered_s = ''.join(c for c in s if c in string.ascii_lowercase)
msg391482 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-04-20 22:34
Please take this to the python-ideas mailing list:  https://mail.python.org/mailman3/lists/python-ideas.python.org/

If the idea gains traction, it would likely require a PEP and then this issue can be reopened.
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 88066
2021-04-20 22:34:59rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg391482

resolution: later
stage: resolved
2021-04-20 22:07:52zach.waresetnosy: + zach.ware
messages: + msg391481
2021-04-20 21:55:08alvarezdqalcreate