Loop inline python code causing a syntax bug

Viewed 176

i'm learning how to write inline code right now i came across this problem

Write an inline for loop in python to remove all the numbers & capital letters from this list of codes and return a clean list. Ex1: input_list = ['1aGpple', '2Uboy', '3cat4', '5li3on3'] output: ['apple', 'boy', 'cat', 'lion']

try 1

a=['1aGpple', '2Uboy', '3cat4', '5li3on3']


t=[c for c in [a[i] for i in range(len(a))] if c.isupper()]
print(str(t))

try 2

a=['1aGpple', '2Uboy', '3cat4', '5li3on3']


t=[ a[i] for i in range(len(a))&&c for c in a[i] if c.isupper()]

if you have good documentation to write inline code please provide me

1 Answers

You can start by splitting the problem in to pieces

words = ['1aGpple', '2Uboy', '3cat4', '5li3on3']

First, iterate the list of strings

for word in words

Now, iterate each and every character of the word

for char in word

Now reconstruct the list of characters by excluding digits and capital letters

[char for char in word if not char.isupper() and not char.isdigit()]

This just gives a list of characters, so you have to join them together

"".join([char for char in word if not char.isupper() and not char.isdigit()])

Now, put them all together

[
    "".join([c for c in word if not c.isupper() and not c.isdigit()])
    for word in words
]

which would produce

['apple', 'boy', 'cat', 'lion']
Related