I have the following code which has for and if alternatively.
lines = ['apple berry Citrus ', 34, 4.46, 'Audi Apple ']
corpus = [ ]
for line in lines:
# Check if the element is string and proceed
if isinstance(line, str):
# Split the element and check if first character is upper case
for word in line.split():
if word[0].isupper():
# Append the word to corpus result
corpus.append(word)
print(corpus)
# Output : ['Citrus', 'Audi', 'Apple']
I am trying to do this in list comprehension but failing. I have tried as below.
# corpus = [ word if word[0].isupper() for word in line.split() for line in lines if isinstance(line, str)]
How can i achieve this in List Comprehension ?