[issue43900] string comprehension

David Alvarez Lombardi report at bugs.python.org
Tue Apr 20 17:55:08 EDT 2021


New submission from David Alvarez Lombardi <alvarezdqal at gmail.com>:

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)
````

----------
messages: 391480
nosy: alvarezdqal
priority: normal
severity: normal
status: open
title: string comprehension
type: enhancement
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43900>
_______________________________________


More information about the Python-bugs-list mailing list