python: add str and str.title() to list using list comprehension -


i reading in file words, this:

stop_words = [x x in open('stopwords.txt', 'r').read().split('\n')] 

but need title() version of word in same list. can using 1 list comprehension?

in 1 (nested) list comprehension:

stop_words = [y x in open('stopwords.txt', 'r').read().split('\n') y in (x, x.title())] 

edit: shouldn't this, because lose file object open file , can't close it. should use context manager:

with open('stopwords.txt', 'r') f:     stop_words = [y x in f.read().split('\n') y in (x, x.title())] 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -