May-13-2020, 11:24 PM
(May-13-2020, 10:52 PM)Pedroski55 Wrote: Is this some new use of a list??It's list comprehensions not new as it has been a part Python language for 20-years PEP 202.
If break it to ordinary loop and append to a new list way,it would look look like this.
>>> words = 'hello123 world' >>> new_lst = [] >>> for w in words: ... if w.isdigit(): ... new_lst.append('X') ... else: ... new_lst.append(w) ... >>> new_lst ['h', 'e', 'l', 'l', 'o', 'X', 'X', 'X', ' ', 'w', 'o', 'r', 'l', 'd']
>>> words = 'hello123 world' >>> [''.join('X' if w.isdigit() else w for w in word) for word in words] ['h', 'e', 'l', 'l', 'o', 'X', 'X', 'X', ' ', 'w', 'o', 'r', 'l', 'd']